Skip to content

Commit

Permalink
fix(logging): fix error when pino pretty is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
uid10804 committed Dec 14, 2019
1 parent 0384e83 commit f698257
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 22 deletions.
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@types/lodash": "4.14.149",
"@types/lowdb": "1.0.9",
"@types/node": "10.14.16",
"@types/pino": "^5.14.0",
"@types/supertest": "2.0.8",
"@types/swagger-schema-official": "2.0.20",
"@types/swagger-ui-express": "4.1.0",
Expand Down
2 changes: 0 additions & 2 deletions serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ custom:
webpackConfig: 'webpack.config.prod.js' # Name of webpack configuration file
includeModules:
packagePath: './package.json'
forceInclude:
- pino-pretty
packager: 'npm' # Packager that will be used to package your external modules
excludeFiles: src/**/*.test.js # Provide a glob for files to ignore
# The `provider` block defines where your service will be deployed
Expand Down
7 changes: 5 additions & 2 deletions src/server/app/core.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class CoreApp {
storageAdapter: StorageAdapter;
static storage = {} as lowdb.AdapterAsync;
static adapter = {} as lowdb.LowdbAsync<{}>;
logger = new Logger().logger;
private prettyPrintLog = false;
appConfig: AppConfig;
protected server: express.Express;
private apispec: ApiSpecification;
Expand All @@ -21,12 +21,15 @@ export class CoreApp {
appConfig: AppConfig,
server: express.Express,
storageAdapter: StorageAdapter,
apispec: ApiSpecification
apispec: ApiSpecification,
prettyPrintLog = false
) {
this.appConfig = appConfig;
this.server = server;
this.storageAdapter = storageAdapter;
this.apispec = apispec;
this.prettyPrintLog = prettyPrintLog;
Logger.init(this.prettyPrintLog);
}

async setup(): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/server/coreserver/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express';
import { Logger } from '../utils/logger';
const logger = new Logger().logger;
const logger = Logger.getInstance();
import { CoreApp } from '../app/core.app';
export abstract class CoreServer {
core: CoreApp;
Expand Down
26 changes: 17 additions & 9 deletions src/server/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export class ServerFactory {
Environment,
new FileStorageAdapter(appConfig.jsonFile),
appConfig,
server
server,
true
);
break;
}
Expand All @@ -37,7 +38,8 @@ export class ServerFactory {
Environment,
new FileStorageAdapter(appConfig.jsonFile),
appConfig,
server
server,
true
);
break;
}
Expand All @@ -49,7 +51,8 @@ export class ServerFactory {
DevEnvironment,
new S3StorageAdapter(environment.s3Bucket, environment.s3File),
appConfig,
server
server,
true
);
break;
}
Expand All @@ -61,7 +64,8 @@ export class ServerFactory {
DevEnvironment,
new S3StorageAdapter(environment.s3Bucket, environment.s3File),
appConfig,
server
server,
true
);
break;
}
Expand All @@ -72,7 +76,8 @@ export class ServerFactory {
Environment,
new FileStorageAdapter(appConfig.jsonFile),
appConfig,
server
server,
true
);
break;
}
Expand All @@ -84,7 +89,8 @@ export class ServerFactory {
CloudEnvironment,
new S3StorageAdapter(environment.s3Bucket, environment.s3File),
appConfig,
server
server,
false
);
break;
}
Expand All @@ -105,13 +111,15 @@ export class ServerFactory {
appConfig: AppConfig,
server: express.Express,
storage: S,
specification: ApiSpecification
specification: ApiSpecification,
prettyPrintLog: boolean
): A;
},
environment: { new (): E },
storage: S,
appConfig: AppConfig,
server: express.Express
server: express.Express,
prettyPrintLog = false
): C {
const env = new environment();
const swagger = new Swagger(
Expand All @@ -121,7 +129,7 @@ export class ServerFactory {
);
const core = new coreserver(
server,
new app(appConfig, server, storage, swagger)
new app(appConfig, server, storage, swagger, prettyPrintLog)
);
return core;
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/specifications/swagger/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Output } from '../../utils/output';
export class Swagger implements ApiSpecification {
private swaggerSpec = new SwaggerSpec();
private swaggerDefGen = new SwaggerDefGen();
private logger = new Logger().logger;
private logger = Logger.getInstance();
private spec = {} as Spec;
private server: express.Express;
private config: SwaggerConfig;
Expand Down
26 changes: 22 additions & 4 deletions src/server/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import * as pino from 'pino';

export class Logger {
logger = require('pino')(
{
prettyPrint: { colorize: true },
private static instance: pino.Logger;
private constructor() { }

public static init(prettyPrint = false){
Logger.instance = pino.default({
prettyPrint: prettyPrint
},
process.stderr
);
}
}

static getInstance():pino.Logger {
if (!Logger.instance) {
return Logger.instance = pino.default({
prettyPrint: false
},
process.stderr
);
}
return Logger.instance;

}
}
6 changes: 3 additions & 3 deletions src/server/utils/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Logger } from './logger';

export class Output {
static setWarning(message: string) {
new Logger().logger.warning(message);
Logger.getInstance().warning(message);
}
static setError(message: string) {
new Logger().logger.error(message);
Logger.getInstance().error(message);
}
static setInfo(message: string) {
new Logger().logger.info(message);
Logger.getInstance().info(message);
}
}

0 comments on commit f698257

Please sign in to comment.