Skip to content

Commit

Permalink
feat(logger): ability to specify custom logger (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 authored Mar 14, 2019
1 parent b328125 commit 0548343
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 6 deletions.
9 changes: 8 additions & 1 deletion examples/2-complex-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Run `yarn bootstrap && yarn start`

## Bootstrapping the App

Running `yarn bootstrap` will do the following:
Running `DEBUG=* yarn bootstrap` will do the following:

- Install packages
- Create the example DB
Expand All @@ -18,3 +18,10 @@ To run the project, run `yarn start`. This will:

- Run the API server
- Open GraphQL Playground

## Features in this example

- Custom logger
- Enums
- Authorization
- Validations
1 change: 1 addition & 0 deletions examples/2-complex-example/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "example2",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"//": "make sure to install top-level packages, too",
"bootstrap": "cd ../.. && yarn && cd - && yarn && yarn db:create && yarn db:seed:dev",
Expand Down
11 changes: 11 additions & 0 deletions examples/2-complex-example/src/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Debug from 'debug';

// TODO: better logger
export const customLogger = {
error: Debug('custom:error'),
info: Debug('custom:info'),
log: Debug('custom:log'),
warn: Debug('custom:warn')
};

type logFunc = (...args: any[]) => void;
3 changes: 3 additions & 0 deletions examples/2-complex-example/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import 'reflect-metadata';

import { BaseContext, Server } from '../../../src/';

import { customLogger } from './logger';

interface Context extends BaseContext {
user: {
email: string;
Expand All @@ -23,6 +25,7 @@ export function getServer(AppOptions = {}, dbOptions = {}) {
}
};
},
logger: customLogger,
// Path written in generated classes (only needed because we're in same repo)
warthogImportPath: '../../../src',
...AppOptions
Expand Down
16 changes: 14 additions & 2 deletions src/core/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ServerOptions<T> {
context?: (request: Request) => object;
host?: string;
generatedFolder?: string;
logger?: Logger;
middlewares?: any[]; // TODO: fix
mockDBConnection?: boolean;
openPlayground?: boolean;
Expand Down Expand Up @@ -81,12 +82,23 @@ export class Server<C extends BaseContext> {
// Use https://github.com/inxilpro/node-app-root-path to find project root
this.generatedFolder = this.appOptions.generatedFolder || path.join(process.cwd(), 'generated');
// Set this so that we can pull in decorators later
Container.set('warthog:generatedFolder', this.generatedFolder);
Container.set('warthog.generated-folder', this.generatedFolder);

this.logger = this.getLogger();
Container.set('warthog.logger', this.logger); // Save for later so we can pull globally

this.logger = Container.has('LOGGER') ? Container.get('LOGGER') : logger;
this.resolversPath = this.appOptions.resolversPath || [process.cwd() + '/**/*.resolver.ts'];
}

getLogger(): Logger {
if (this.appOptions.logger) {
return this.appOptions.logger;
} else if (Container.has('warthog.logger')) {
return Container.get('warthog.logger');
}
return logger;
}

async establishDBConnection(): Promise<Connection> {
if (!this.connection) {
// Asking for a mock connection will not connect to your preferred DB and will instead
Expand Down
2 changes: 1 addition & 1 deletion src/decorators/EnumField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Enums', () => {

beforeAll(async () => {
// TODO: should we set this up as part of the test harness?
Container.set('warthog:generatedFolder', process.cwd());
Container.set('warthog.generated-folder', process.cwd());

enum StringEnum {
Foo = 'FOO',
Expand Down
4 changes: 3 additions & 1 deletion src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import {
} from './TypeORMConverter';

export class SchemaGenerator {
static logger: Logger = Container.has('LOGGER') ? Container.get('LOGGER') : logger;
static logger: Logger = Container.has('warthog.logger')
? Container.get('warthog.logger')
: logger;

static generate(
entities: EntityMetadata[],
Expand Down
2 changes: 1 addition & 1 deletion src/utils/generatedFolder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Container } from 'typedi';

export const generatedFolderPath = (): string => {
try {
return Container.get('warthog:generatedFolder');
return Container.get('warthog.generated-folder');
} catch (error) {
return path.join(appRoot.path, 'generated');
}
Expand Down

0 comments on commit 0548343

Please sign in to comment.