Merge pull request #16097 from mozilla/feat/add-winston-logging

feat: add winston logger and nest helper
This commit is contained in:
Ben Bangert 2023-11-30 13:48:02 -08:00 коммит произвёл GitHub
Родитель 3ec8e283cd 3878034f0b
Коммит f869cfa3e2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 273 добавлений и 40 удалений

Просмотреть файл

@ -7,7 +7,7 @@ import { createPool } from 'mysql2';
import { promisify } from 'util';
import { v4 as uuidv4 } from 'uuid';
import { ConsoleLogger, Logger } from '@fxa/shared/log';
import { logger, Logger } from '@fxa/shared/log';
import { localStatsD, StatsD } from '@fxa/shared/metrics/statsd';
import { MySQLConfig } from './config';
@ -77,7 +77,7 @@ export function monitorKnexConnectionPool(pool: any, metrics: StatsD) {
*/
export function createKnex(
opts: MySQLConfig,
log: Logger = new ConsoleLogger(),
log: Logger = logger,
metrics: StatsD = localStatsD()
): Knex {
const db = knex({
@ -111,7 +111,7 @@ export function generateFxAUuid() {
export async function createDialect(
opts: MySQLConfig,
log: Logger = new ConsoleLogger(),
log: Logger = logger,
metrics: StatsD = localStatsD()
) {
log.debug('mysqlDialect', {

12
libs/shared/log/LICENSE Normal file
Просмотреть файл

@ -0,0 +1,12 @@
Source files without the MPL notice at the top in this directory and below are from:
https://github.com/island-is/island.is/tree/main/libs/logging
and are MIT licensed.
Copyright 2020 Digital Iceland
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Просмотреть файл

@ -2,5 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
export { ConsoleLogger } from './lib/log';
export type { Logger } from './lib/log';
export { logger } from './lib/logging';
export { monkeyPatchServerLogging } from './lib/monkey-patch';
export { LoggingModule } from './lib/nest/logging.module';
export type { Logger } from './lib/nest/logging.module';

Просмотреть файл

@ -1,32 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
export interface Logger {
info: (...args: any) => void;
trace: (...args: any) => void;
warn: (...args: any) => void;
error: (...args: any) => void;
debug: (...args: any) => void;
}
/**
* A logger that logs to the console.
*/
export class ConsoleLogger {
public info(...args: any) {
console.log(...args);
}
public trace(...args: any) {
console.log(...args);
}
public warn(...args: any) {
console.log(...args);
}
public error(...args: any) {
console.log(...args);
}
public debug(...args: any) {
console.log(...args);
}
}

Просмотреть файл

@ -0,0 +1,38 @@
import { createLogger, format, LoggerOptions, transports } from 'winston';
import { utilities } from 'nest-winston';
// Default log settings for debug mode
let logLevel = 'debug';
let logFormat = format.combine(
format.errors({ stack: true }),
format.timestamp(),
utilities.format.nestLike('App')
);
// Production overrides
if (process.env.NODE_ENV === 'production') {
logLevel = process.env.LOG_LEVEL || 'info';
logFormat = format.combine(
format.errors({ stack: true }),
format.timestamp(),
format.json()
);
}
const logTransports = [new transports.Console()];
// Turn on global winston exception handling
const exceptionHandling = {} as Partial<LoggerOptions>;
if (process.env.WINSTON_LOGGING === 'true') {
exceptionHandling['exceptionHandlers'] = logTransports;
exceptionHandling['rejectionHandlers'] = logTransports;
}
export const logger = createLogger({
level: logLevel,
format: logFormat,
transports: logTransports,
handleExceptions: true,
exitOnError: true,
...exceptionHandling,
} as LoggerOptions);

Просмотреть файл

@ -0,0 +1,38 @@
import { logger } from './logging';
type ConsoleFn = typeof console.log;
// Wrap console logs to convert multiple arguments to something winston supports.
const consoleLogWrapper = (logFn: ConsoleFn) =>
function (message: unknown, extra: unknown) {
// hot paths.
const hasMessage = typeof message === 'string';
if (arguments.length === 1 && hasMessage) {
return logFn(message);
} else if (
arguments.length === 2 &&
hasMessage &&
typeof extra === 'object'
) {
return logFn(message, extra);
}
// eslint-disable-next-line prefer-rest-params
const args = Array.from(arguments);
const strings = args.filter((arg) => typeof arg === 'string');
const objects = args.filter((arg) => typeof arg === 'object');
message = strings.join(' ');
extra = objects[0];
if (objects.length > 1) {
extra = Object.assign({}, ...objects);
}
logFn(message, extra);
};
export const monkeyPatchServerLogging = () => {
// NextJS internally uses console functions directly, eg on error. It's also
// rare to load a logging library in react code.
console.log = consoleLogWrapper(logger.info.bind(logger));
console.warn = consoleLogWrapper(logger.warn.bind(logger));
console.error = consoleLogWrapper(logger.error.bind(logger));
};

Просмотреть файл

@ -0,0 +1,18 @@
import { Global, LoggerService, Module } from '@nestjs/common';
export type { Logger } from 'winston';
import { WinstonLogger } from 'nest-winston';
import { logger } from '../logging';
export const LOGGER_PROVIDER = 'Logger';
@Global()
@Module({
providers: [{ provide: LOGGER_PROVIDER, useValue: logger }],
exports: [{ provide: LOGGER_PROVIDER, useValue: logger }],
})
export class LoggingModule {
static createLogger(): LoggerService {
return new WinstonLogger(logger);
}
}

Просмотреть файл

@ -67,6 +67,7 @@
"mysql": "^2.18.1",
"mysql2": "^3.6.0",
"nest-typed-config": "^2.9.1",
"nest-winston": "^1.9.4",
"next": "^14.0.2",
"node-fetch": "^2.6.7",
"nps": "^5.10.0",
@ -82,7 +83,8 @@
"rxjs": "^7.8.1",
"semver": "^7.5.2",
"tslib": "^2.5.0",
"uuid": "^9.0.0"
"uuid": "^9.0.0",
"winston": "^3.11.0"
},
"engines": {
"node": "^18.18.2"

Просмотреть файл

@ -175,6 +175,9 @@ function createMockRedis() {
this.redis = this;
return this;
},
info() {
return 'mock\nredis';
},
async set(key, value, opt, ttl) {
_data[key] = value;
},
@ -186,6 +189,7 @@ function createMockRedis() {
},
};
Object.keys(mock).forEach((key) => sinon.spy(mock, key));
mock.options = {};
return mock;
}

155
yarn.lock
Просмотреть файл

@ -6000,6 +6000,13 @@ __metadata:
languageName: node
linkType: hard
"@colors/colors@npm:1.6.0, @colors/colors@npm:^1.6.0":
version: 1.6.0
resolution: "@colors/colors@npm:1.6.0"
checksum: aa209963e0c3218e80a4a20553ba8c0fbb6fa13140540b4e5f97923790be06801fc90172c1114fc8b7e888b3d012b67298cde6b9e81521361becfaee400c662f
languageName: node
linkType: hard
"@cspotcode/source-map-support@npm:^0.8.0":
version: 0.8.1
resolution: "@cspotcode/source-map-support@npm:0.8.1"
@ -6651,6 +6658,17 @@ __metadata:
languageName: node
linkType: hard
"@dabh/diagnostics@npm:^2.0.2":
version: 2.0.3
resolution: "@dabh/diagnostics@npm:2.0.3"
dependencies:
colorspace: 1.1.x
enabled: 2.0.x
kuler: ^2.0.0
checksum: 4879600c55c8315a0fb85fbb19057bad1adc08f0a080a8cb4e2b63f723c379bfc4283b68123a2b078d367b327dd8df12fcb27464efe791addc0a48b9df6d79a1
languageName: node
linkType: hard
"@discoveryjs/json-ext@npm:^0.5.0, @discoveryjs/json-ext@npm:^0.5.3":
version: 0.5.3
resolution: "@discoveryjs/json-ext@npm:0.5.3"
@ -20643,6 +20661,13 @@ __metadata:
languageName: node
linkType: hard
"@types/triple-beam@npm:^1.3.2":
version: 1.3.5
resolution: "@types/triple-beam@npm:1.3.5"
checksum: 519b6a1b30d4571965c9706ad5400a200b94e4050feca3e7856e3ea7ac00ec9903e32e9a10e2762d0f7e472d5d03e5f4b29c16c0bd8c1f77c8876c683b2231f1
languageName: node
linkType: hard
"@types/trusted-types@npm:^2.0.2":
version: 2.0.3
resolution: "@types/trusted-types@npm:2.0.3"
@ -27523,7 +27548,7 @@ __metadata:
languageName: node
linkType: hard
"color@npm:^3.1.2":
"color@npm:^3.1.2, color@npm:^3.1.3":
version: 3.2.1
resolution: "color@npm:3.2.1"
dependencies:
@ -27592,6 +27617,16 @@ __metadata:
languageName: node
linkType: hard
"colorspace@npm:1.1.x":
version: 1.1.4
resolution: "colorspace@npm:1.1.4"
dependencies:
color: ^3.1.3
text-hex: 1.0.x
checksum: bb3934ef3c417e961e6d03d7ca60ea6e175947029bfadfcdb65109b01881a1c0ecf9c2b0b59abcd0ee4a0d7c1eae93beed01b0e65848936472270a0b341ebce8
languageName: node
linkType: hard
"columnify@npm:^1.6.0":
version: 1.6.0
resolution: "columnify@npm:1.6.0"
@ -30722,6 +30757,13 @@ __metadata:
languageName: node
linkType: hard
"enabled@npm:2.0.x":
version: 2.0.0
resolution: "enabled@npm:2.0.0"
checksum: 9d256d89f4e8a46ff988c6a79b22fa814b4ffd82826c4fdacd9b42e9b9465709d3b748866d0ab4d442dfc6002d81de7f7b384146ccd1681f6a7f868d2acca063
languageName: node
linkType: hard
"encode-utf8@npm:^1.0.3":
version: 1.0.3
resolution: "encode-utf8@npm:1.0.3"
@ -33370,6 +33412,13 @@ __metadata:
languageName: node
linkType: hard
"fecha@npm:^4.2.0":
version: 4.2.3
resolution: "fecha@npm:4.2.3"
checksum: f94e2fb3acf5a7754165d04549460d3ae6c34830394d20c552197e3e000035d69732d74af04b9bed3283bf29fe2a9ebdcc0085e640b0be3cc3658b9726265e31
languageName: node
linkType: hard
"fetch-mock@npm:^9.11.0":
version: 9.11.0
resolution: "fetch-mock@npm:9.11.0"
@ -33951,6 +34000,13 @@ __metadata:
languageName: node
linkType: hard
"fn.name@npm:1.x.x":
version: 1.1.0
resolution: "fn.name@npm:1.1.0"
checksum: e357144f48cfc9a7f52a82bbc6c23df7c8de639fce049cac41d41d62cabb740cdb9f14eddc6485e29c933104455bdd7a69bb14a9012cef9cd4fa252a4d0cf293
languageName: node
linkType: hard
"follow-redirects@npm:^1.0.0, follow-redirects@npm:^1.14.0, follow-redirects@npm:^1.14.8, follow-redirects@npm:^1.14.9, follow-redirects@npm:^1.15.0":
version: 1.15.2
resolution: "follow-redirects@npm:1.15.2"
@ -35833,6 +35889,7 @@ fsevents@~2.1.1:
mysql: ^2.18.1
mysql2: ^3.6.0
nest-typed-config: ^2.9.1
nest-winston: ^1.9.4
next: ^14.0.2
node-fetch: ^2.6.7
nps: ^5.10.0
@ -35862,6 +35919,7 @@ fsevents@~2.1.1:
tslib: ^2.5.0
typescript: ^5.2.2
uuid: ^9.0.0
winston: ^3.11.0
languageName: unknown
linkType: soft
@ -43966,6 +44024,13 @@ fsevents@~2.1.1:
languageName: node
linkType: hard
"kuler@npm:^2.0.0":
version: 2.0.0
resolution: "kuler@npm:2.0.0"
checksum: 9e10b5a1659f9ed8761d38df3c35effabffbd19fc6107324095238e4ef0ff044392cae9ac64a1c2dda26e532426485342226b93806bd97504b174b0dcf04ed81
languageName: node
linkType: hard
"kysely@npm:^0.26.1":
version: 0.26.1
resolution: "kysely@npm:0.26.1"
@ -44778,6 +44843,20 @@ fsevents@~2.1.1:
languageName: node
linkType: hard
"logform@npm:^2.3.2, logform@npm:^2.4.0":
version: 2.6.0
resolution: "logform@npm:2.6.0"
dependencies:
"@colors/colors": 1.6.0
"@types/triple-beam": ^1.3.2
fecha: ^4.2.0
ms: ^2.1.1
safe-stable-stringify: ^2.3.1
triple-beam: ^1.3.0
checksum: b9ea74bb75e55379ad0eb3e4d65ae6e8d02bc45b431c218162878bf663997ab9258a73104c2b30e09dd2db288bb83c8bf8748e46689d75f5e7e34cf69378d6df
languageName: node
linkType: hard
"loglevel@npm:^1.6.8":
version: 1.8.1
resolution: "loglevel@npm:1.8.1"
@ -47421,6 +47500,18 @@ fsevents@~2.1.1:
languageName: node
linkType: hard
"nest-winston@npm:^1.9.4":
version: 1.9.4
resolution: "nest-winston@npm:1.9.4"
dependencies:
fast-safe-stringify: ^2.1.1
peerDependencies:
"@nestjs/common": ^5.0.0 || ^6.6.0 || ^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0
winston: ^3.0.0
checksum: 595d23135b4760d7ac18613acf018dff538f8167eac7052a473c4995a6609c3584270dd4d82d6a4b3d7fba688284c04e33b93479bbe933a1e2f27d1cd964a681
languageName: node
linkType: hard
"netmask@npm:^2.0.1":
version: 2.0.2
resolution: "netmask@npm:2.0.2"
@ -48885,6 +48976,15 @@ fsevents@~2.1.1:
languageName: node
linkType: hard
"one-time@npm:^1.0.0":
version: 1.0.0
resolution: "one-time@npm:1.0.0"
dependencies:
fn.name: 1.x.x
checksum: fd008d7e992bdec1c67f53a2f9b46381ee12a9b8c309f88b21f0223546003fb47e8ad7c1fd5843751920a8d276c63bd4b45670ef80c61fb3e07dbccc962b5c7d
languageName: node
linkType: hard
"onetime@npm:^2.0.0":
version: 2.0.1
resolution: "onetime@npm:2.0.1"
@ -55407,6 +55507,13 @@ resolve@1.1.7:
languageName: node
linkType: hard
"safe-stable-stringify@npm:^2.3.1":
version: 2.4.3
resolution: "safe-stable-stringify@npm:2.4.3"
checksum: 3aeb64449706ee1f5ad2459fc99648b131d48e7a1fbb608d7c628020177512dc9d94108a5cb61bbc953985d313d0afea6566d243237743e02870490afef04b43
languageName: node
linkType: hard
"safe-url-assembler@npm:1.3.5":
version: 1.3.5
resolution: "safe-url-assembler@npm:1.3.5"
@ -57177,7 +57284,7 @@ resolve@1.1.7:
languageName: node
linkType: hard
"stack-trace@npm:^0.0.10, stack-trace@npm:~0.0.9":
"stack-trace@npm:0.0.x, stack-trace@npm:^0.0.10, stack-trace@npm:~0.0.9":
version: 0.0.10
resolution: "stack-trace@npm:0.0.10"
checksum: 473036ad32f8c00e889613153d6454f9be0536d430eb2358ca51cad6b95cea08a3cc33cc0e34de66b0dad221582b08ed2e61ef8e13f4087ab690f388362d6610
@ -59166,6 +59273,13 @@ resolve@1.1.7:
languageName: node
linkType: hard
"text-hex@npm:1.0.x":
version: 1.0.0
resolution: "text-hex@npm:1.0.0"
checksum: 1138f68adc97bf4381a302a24e2352f04992b7b1316c5003767e9b0d3367ffd0dc73d65001ea02b07cd0ecc2a9d186de0cf02f3c2d880b8a522d4ccb9342244a
languageName: node
linkType: hard
"text-table@npm:0.2.0, text-table@npm:^0.2.0":
version: 0.2.0
resolution: "text-table@npm:0.2.0"
@ -59718,6 +59832,13 @@ resolve@1.1.7:
languageName: node
linkType: hard
"triple-beam@npm:^1.3.0":
version: 1.4.1
resolution: "triple-beam@npm:1.4.1"
checksum: 2e881a3e8e076b6f2b85b9ec9dd4a900d3f5016e6d21183ed98e78f9abcc0149e7d54d79a3f432b23afde46b0885bdcdcbff789f39bc75de796316961ec07f61
languageName: node
linkType: hard
"trivial-deferred@npm:^1.0.1":
version: 1.0.1
resolution: "trivial-deferred@npm:1.0.1"
@ -62530,6 +62651,36 @@ resolve@1.1.7:
languageName: node
linkType: hard
"winston-transport@npm:^4.5.0":
version: 4.6.0
resolution: "winston-transport@npm:4.6.0"
dependencies:
logform: ^2.3.2
readable-stream: ^3.6.0
triple-beam: ^1.3.0
checksum: 19f06ebdbb57cb14cdd48a23145d418d3bbe538851053303f84f04a8a849bb530b78b1495a175059c1299f92945dc61d5421c4914fee32d9a41bc397d84f26d7
languageName: node
linkType: hard
"winston@npm:^3.11.0":
version: 3.11.0
resolution: "winston@npm:3.11.0"
dependencies:
"@colors/colors": ^1.6.0
"@dabh/diagnostics": ^2.0.2
async: ^3.2.3
is-stream: ^2.0.0
logform: ^2.4.0
one-time: ^1.0.0
readable-stream: ^3.4.0
safe-stable-stringify: ^2.3.1
stack-trace: 0.0.x
triple-beam: ^1.3.0
winston-transport: ^4.5.0
checksum: ca4454070f7a71b19f53c8c1765c59a013dab220edb49161b2e81917751d3e9edc3382430e4fb050feda04fb8463290ecab7cbc9240ec8d3d3b32a121849bbb0
languageName: node
linkType: hard
"word-wrap@npm:^1.2.3, word-wrap@npm:~1.2.3":
version: 1.2.5
resolution: "word-wrap@npm:1.2.5"