Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: fix logger init #6

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions examples/replace-nest-logger-bootstrap/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { LoggerAdapter } from '@wildegor/nestjs-prologger/modules/modules/logger/logger.adapter';
import { LoggerConstants } from '@wildegor/nestjs-prologger/modules/modules/logger/logger.constants';
import { LogLevels } from '@wildegor/nestjs-prologger/modules/infrastructure/interfaces/logger.interfaces';
import { LoggerAdapter } from '../../../src/modules/logger/logger.adapter';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(AppModule, {
bufferLogs: true,
});

const logger = app.get<LoggerAdapter>(LoggerConstants.logger);
const logger = app.get<LoggerAdapter>(LoggerAdapter);
logger.setLogLevels([
LogLevels.Debug,
LogLevels.Log,
LogLevels.Warn,
LogLevels.Verbose,
]);

app.useLogger(logger);

logger.error('Error message');

throw Error('Error message');

await app.listen(3001);
}
bootstrap();
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
import * as winston from 'winston';
import { LogColors, LogLevels } from '../../../../interfaces/logger.interfaces';
import { IConsoleTransportOpts, LogColors, LogLevels } from '../../../../interfaces/logger.interfaces';

export class ConsoleTransport {

public static create(): winston.transports.ConsoleTransportInstance {
public static create(opts?: IConsoleTransportOpts): winston.transports.ConsoleTransportInstance {
if (opts?.format && 'json' === opts.format) {
return new winston.transports.Console({
format: winston.format.json({
deterministic: true,
}),
});
}

return new winston.transports.Console({
format: winston.format.combine(
winston.format.printf((log) => {
winston.format.printf(log => {
const color = this._mapLogLevelColor(log.level as LogLevels);

const prefix = `${log?.data?.label ? `[${log.data.label}]` : ''}`;
const prefix = `${log.data?.label ? `[${log.data.label}]` : ''}`;

Check warning on line 20 in src/infrastructure/adapters/winston/transports/console/console.transport.ts

View workflow job for this annotation

GitHub Actions / Check

Unsafe member access .label on an `any` value

Check warning on line 20 in src/infrastructure/adapters/winston/transports/console/console.transport.ts

View workflow job for this annotation

GitHub Actions / Check

Unsafe member access .label on an `any` value

return `${this._colorize(color, `${prefix} -`)} ${log.timestamp} ${
log?.data?.correlationId
log.data?.correlationId

Check warning on line 23 in src/infrastructure/adapters/winston/transports/console/console.transport.ts

View workflow job for this annotation

GitHub Actions / Check

Unsafe member access .correlationId on an `any` value
? `(${this._colorize(LogColors.cyan, log.data.correlationId)})`

Check warning on line 24 in src/infrastructure/adapters/winston/transports/console/console.transport.ts

View workflow job for this annotation

GitHub Actions / Check

Unsafe member access .correlationId on an `any` value
: ''
} ${this._colorize(color, log.level.toUpperCase())} ${
log?.data?.source
log.data?.source

Check warning on line 27 in src/infrastructure/adapters/winston/transports/console/console.transport.ts

View workflow job for this annotation

GitHub Actions / Check

Unsafe member access .source on an `any` value
? `${this._colorize(LogColors.cyan, `[${log.data.source}]`)}`

Check warning on line 28 in src/infrastructure/adapters/winston/transports/console/console.transport.ts

View workflow job for this annotation

GitHub Actions / Check

Unsafe member access .source on an `any` value
: ''
} ${this._colorize(
color,
`${log.message} - ${log.data.error ? log.data.error : ''}`,

Check warning on line 32 in src/infrastructure/adapters/winston/transports/console/console.transport.ts

View workflow job for this annotation

GitHub Actions / Check

Unsafe member access .error on an `any` value

Check warning on line 32 in src/infrastructure/adapters/winston/transports/console/console.transport.ts

View workflow job for this annotation

GitHub Actions / Check

Unsafe member access .error on an `any` value
)}${
typeof log?.data?.durationMs !== 'undefined'
typeof log.data?.durationMs !== 'undefined'

Check warning on line 34 in src/infrastructure/adapters/winston/transports/console/console.transport.ts

View workflow job for this annotation

GitHub Actions / Check

Unsafe member access .durationMs on an `any` value
? this._colorize(color, ` +${log.data.durationMs}ms`)

Check warning on line 35 in src/infrastructure/adapters/winston/transports/console/console.transport.ts

View workflow job for this annotation

GitHub Actions / Check

Unsafe member access .durationMs on an `any` value
: ''
}${
log?.data?.stack ? this._colorize(color, ` - ${log.data.stack}`) : ''
log.data?.stack ? this._colorize(color, ` - ${log.data.stack}`) : ''
}${
log?.data?.props
log.data?.props
? `\n - Props: ${JSON.stringify(log.data.props, null, 4)}`
: ''
}`;
Expand Down
36 changes: 26 additions & 10 deletions src/infrastructure/adapters/winston/winston.adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import * as winston from 'winston';
import { LoggerOptions } from 'winston';
import { LogEntry, LoggerOptions } from 'winston';
import { InjectLoggerOpts } from '../../../modules/logger/logger.decorators';
import {
ILogPayload,
Expand Down Expand Up @@ -51,12 +51,16 @@ export class WinstonAdapter implements ILoggerPort {
let cont = 0;

// Set all levels
if (!levels) {
if ('undefined' === typeof levels) {
for (const level of Object.values(LogLevels)) {
if (level === LogLevels.Silent) {
continue;
}

if (level === LogLevels.Log) {
this._logLevels.add(LogLevels.Info);
}

this._logLevels.add(level);
this._defaultLevels[level] = cont;
cont += 1;
Expand All @@ -66,6 +70,10 @@ export class WinstonAdapter implements ILoggerPort {
}

for (const level of levels) {
if (level === LogLevels.Log) {
this._logLevels.add(LogLevels.Info);
}

this._logLevels.add(level);
this._defaultLevels[level] = cont;
cont += 1;
Expand All @@ -79,34 +87,42 @@ export class WinstonAdapter implements ILoggerPort {
}

public verbose(message: string | Error, data?: ILogPayload | undefined, profile?: string | undefined): void {
this.log(LogLevels.Verbose, message, data, profile);
return this.print(LogLevels.Verbose, message, data, profile);
}

public debug(message: string, data?: ILogPayload, profile?: string): void {
this.log(LogLevels.Debug, message, data, profile);
return this.print(LogLevels.Debug, message, data, profile);
}

public info(message: string, data?: ILogPayload, profile?: string): void {
this.log(LogLevels.Info, message, data, profile);
return this.print(LogLevels.Info, message, data, profile);
}

public warn(message: string | Error, data?: ILogPayload, profile?: string): void {
this.log(LogLevels.Warn, message, data, profile);
return this.print(LogLevels.Warn, message, data, profile);
}

public error(message: string | Error, data?: ILogPayload, profile?: string): void {
this.log(LogLevels.Error, message, data, profile);
return this.print(LogLevels.Error, message, data, profile);
}

public emergency(message: string | Error, data?: ILogPayload, profile?: string): void {
this.log(LogLevels.Emergency, message, data, profile);
return this.print(LogLevels.Emergency, message, data, profile);
}

public fatal(message: string | Error, data?: ILogPayload, profile?: string): void {
this.log(LogLevels.Emergency, message, data, profile);
return this.print(LogLevels.Emergency, message, data, profile);
}

public log(
message: string | Error,
data?: ILogPayload,
profile?: string,
): void {
return this.print(LogLevels.Info, message, data, profile);
}

public print(
level: LogLevels,
message: string | Error,
data?: ILogPayload,
Expand All @@ -116,7 +132,7 @@ export class WinstonAdapter implements ILoggerPort {
return;
}

const logData = {
const logData: LogEntry = {
level,
message: message instanceof Error ? message.message : message,
error: message instanceof Error ? message : undefined,
Expand Down
31 changes: 25 additions & 6 deletions src/infrastructure/interfaces/logger.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ export interface IFileTransportOpts {
period: string;
}

export interface IConsoleTransportOpts {
format: 'json' | 'pretty';
}

export interface ITelegramFormatOptions {
context: string;
timestamp: string;
Expand Down Expand Up @@ -38,13 +42,16 @@ export interface ITelegramTransportOpts {
batchingSeparator?: string;
}

export interface ILoggerMetaOptions {
organization?: string;
context?: string;
app?: string;
}

export interface ILoggerOptions {
hideTrace?: boolean;
meta?: {
organization?: string;
context?: string;
app?: string;
},
meta?: ILoggerMetaOptions;
console?: IConsoleTransportOpts;
file?: IFileTransportOpts;
telegram?: ITelegramTransportOpts;
}
Expand Down Expand Up @@ -117,13 +124,25 @@ export interface ILoggerPort {
* @param data
* @param profile
*/
log(
print(
level: LogLevel,
message: string | Error,
data?: ILogPayload,
profile?: string,
): void;

/**
* @description Log a message with level `log`
* @param message
* @param data
* @param profile
*/
log(
message: string | Error,
data?: ILogPayload,
profile?: string,
): void;

/**
* @description Log a message with level `debug` and color `blue`
* @param message
Expand Down
9 changes: 7 additions & 2 deletions src/modules/logger/logger.adapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConsoleLogger } from '@nestjs/common';
import { LoggerService } from '@nestjs/common/services/logger.service';
import { ILoggerPort, ILogPayload } from '../../infrastructure/interfaces/logger.interfaces';
import { ILoggerPort, ILogPayload, LogLevel } from '../../infrastructure/interfaces/logger.interfaces';

export class LoggerAdapter
extends ConsoleLogger
Expand All @@ -10,6 +10,10 @@ export class LoggerAdapter
super();
}

public setLogLevels(levels: LogLevel[]): void {
this.logger.setLogLevels(levels);
}

public log(message: any, ...optionalParams: any[]): void {
return this.logger.info(message, this.getLogData(optionalParams));
}
Expand All @@ -31,8 +35,9 @@ export class LoggerAdapter
}

private getLogData(...optionalParams: any[]): ILogPayload {
const source = optionalParams[0]?.length ? optionalParams[0] : this.context;
return {
source: optionalParams[0] ? optionalParams[0] : undefined,
source,
};
}

Expand Down
12 changes: 10 additions & 2 deletions src/modules/logger/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export class LoggerService implements ILoggerPort {
this._contextRepository = contextRepository;
}

public log(message: string | Error, data?: ILogPayload | undefined, profile?: string | undefined): void {
if (message instanceof Error) {
return this._logger.info(message.message, this._extractLogPayload(data), profile);
}

this._logger.info(message, this._extractLogPayload(data), profile);
}

public startProfile(id: string): void {
this._logger.startProfile(id);
}
Expand Down Expand Up @@ -70,13 +78,13 @@ export class LoggerService implements ILoggerPort {
return this._logger.fatal(message, this._extractLogPayload(data), profile);
}

public log(
public print(
level: LogLevel,
message: string | Error,
data?: ILogPayload,
profile?: string,
): void {
return this._logger.log(level, message, this._extractLogPayload(data), profile);
return this._logger.print(level, message, this._extractLogPayload(data), profile);
}

private _extractLogPayload(data?: ILogPayload): ILogPayload {
Expand Down