diff --git a/package-lock.json b/package-lock.json index 545dc4e4..b5f09060 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1416,6 +1416,23 @@ "resolved": "https://registry.npmjs.org/@types/node/-/node-10.14.16.tgz", "integrity": "sha512-/opXIbfn0P+VLt+N8DE4l8Mn8rbhiJgabU96ZJ0p9mxOkIks5gh6RUnpHak7Yh0SFkyjO/ODbxsQQPV2bpMmyA==" }, + "@types/pino": { + "version": "5.14.0", + "resolved": "https://registry.npmjs.org/@types/pino/-/pino-5.14.0.tgz", + "integrity": "sha512-i4jTU0G9HL/wy7WbVMqcM/5Z6cFzbcLN6/T46rqQauIoiDqcEySR7SN4FfJvtv+RoBtJkQjDneq0Ytrj95cQHg==", + "dev": true, + "requires": { + "@types/node": "*", + "@types/pino-std-serializers": "*", + "@types/sonic-boom": "*" + } + }, + "@types/pino-std-serializers": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/@types/pino-std-serializers/-/pino-std-serializers-2.4.0.tgz", + "integrity": "sha512-eAdu+NW1IkCdmp85SnhyKha+OOREQMT9lXaoICQxa7bhSauRiLzu3WSNt9Mf2piuJvWeXF/G0hGWHr63xNpIRA==", + "dev": true + }, "@types/range-parser": { "version": "1.2.3", "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", @@ -1446,6 +1463,15 @@ "@types/mime": "*" } }, + "@types/sonic-boom": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/@types/sonic-boom/-/sonic-boom-0.7.0.tgz", + "integrity": "sha512-AfqR0fZMoUXUNwusgXKxcE9DPlHNDHQp6nKYUd4PSRpLobF5CCevSpyTEBcVZreqaWKCnGBr9KI1fHMTttoB7A==", + "dev": true, + "requires": { + "@types/node": "*" + } + }, "@types/stack-utils": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-1.0.1.tgz", diff --git a/package.json b/package.json index 0f18e10b..51577dcc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/serverless.yml b/serverless.yml index 8665352a..5668d52d 100644 --- a/serverless.yml +++ b/serverless.yml @@ -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 diff --git a/src/server/app/core.app.ts b/src/server/app/core.app.ts index 6f83b9ab..a9438e09 100644 --- a/src/server/app/core.app.ts +++ b/src/server/app/core.app.ts @@ -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; @@ -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 { diff --git a/src/server/coreserver/server.ts b/src/server/coreserver/server.ts index 58577d2b..abad625e 100644 --- a/src/server/coreserver/server.ts +++ b/src/server/coreserver/server.ts @@ -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; diff --git a/src/server/factory.ts b/src/server/factory.ts index b4676b1c..a8b9df52 100644 --- a/src/server/factory.ts +++ b/src/server/factory.ts @@ -26,7 +26,8 @@ export class ServerFactory { Environment, new FileStorageAdapter(appConfig.jsonFile), appConfig, - server + server, + true ); break; } @@ -37,7 +38,8 @@ export class ServerFactory { Environment, new FileStorageAdapter(appConfig.jsonFile), appConfig, - server + server, + true ); break; } @@ -49,7 +51,8 @@ export class ServerFactory { DevEnvironment, new S3StorageAdapter(environment.s3Bucket, environment.s3File), appConfig, - server + server, + true ); break; } @@ -61,7 +64,8 @@ export class ServerFactory { DevEnvironment, new S3StorageAdapter(environment.s3Bucket, environment.s3File), appConfig, - server + server, + true ); break; } @@ -72,7 +76,8 @@ export class ServerFactory { Environment, new FileStorageAdapter(appConfig.jsonFile), appConfig, - server + server, + true ); break; } @@ -84,7 +89,8 @@ export class ServerFactory { CloudEnvironment, new S3StorageAdapter(environment.s3Bucket, environment.s3File), appConfig, - server + server, + false ); break; } @@ -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( @@ -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; } diff --git a/src/server/specifications/swagger/swagger.ts b/src/server/specifications/swagger/swagger.ts index 8c1c6262..b9406478 100644 --- a/src/server/specifications/swagger/swagger.ts +++ b/src/server/specifications/swagger/swagger.ts @@ -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; diff --git a/src/server/utils/logger.ts b/src/server/utils/logger.ts index b6cc4f8e..e2314d2e 100644 --- a/src/server/utils/logger.ts +++ b/src/server/utils/logger.ts @@ -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; + + } +} \ No newline at end of file diff --git a/src/server/utils/output.ts b/src/server/utils/output.ts index fb898273..b086a91c 100644 --- a/src/server/utils/output.ts +++ b/src/server/utils/output.ts @@ -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); } }