fix: compatibility with log aggregators

This commit is contained in:
Rainer Killinger
2022-11-01 18:35:26 +01:00
parent 107d94d499
commit 8aef5b8d5b
8 changed files with 54 additions and 26 deletions

View File

@@ -13,7 +13,7 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {stringify} from 'flatted';
import {isNodeEnvironment, isProductiveNodeEnvironment} from './common';
import {isNodeEnvironment, isProductiveEnvironment, isProductiveNodeEnvironment} from './common';
import {Transformation} from './transformation';
import {AddLogLevel} from './transformations/add-log-level';
import {Transport} from './transport';
@@ -91,17 +91,24 @@ export class Logger {
/**
* Apply transformations to an output
* Will strip newlines in production environment
*
* @param logLevel Log level of the output
* @param output Output to apply transformations to
*/
private static applyTransformers(logLevel: LogLevel, output: string): string {
if (isProductiveEnvironment()) {
output = output.replace(/[\n\r]/g, ' ');
}
if (!Array.isArray(Logger.transformations) || Logger.transformations.length === 0) {
return output;
}
let transformedOutput = output;
for (const transformation of Logger.transformations) {
for (const transformation of Logger.transformations.filter(transform =>
!isProductiveEnvironment() ? true : transform.useInProduction === isProductiveEnvironment(),
)) {
transformedOutput = transformation.transform(logLevel, transformedOutput);
}

View File

@@ -18,6 +18,11 @@ import {LogLevel} from './logger';
* A transformer for log output
*/
export interface Transformation {
/**
* Indicates if this transformation is stripped in production environments
*/
useInProduction: boolean;
/**
* Transform an output
*

View File

@@ -19,6 +19,11 @@ import {Transformation} from '../transformation';
* Transformation that adds the log level to output
*/
export class AddLogLevel implements Transformation {
/**
* Keep this transformation in production environments
*/
useInProduction = true;
/**
* Add log level to output
*

View File

@@ -20,6 +20,11 @@ import {Transformation} from '../transformation';
* Transformation that colorizes log output
*/
export class Colorize implements Transformation {
/**
* Skip this transformation in production environments
*/
useInProduction = false;
/**
* Instantiate a new colorize transformation
*

View File

@@ -14,21 +14,15 @@
*/
import {LogLevel} from '../logger';
import {Transformation} from '../transformation';
import moment from 'moment';
/**
* Transformation that adds a timestamp to output
*/
export class Timestamp implements Transformation {
/**
* Instantiate a new timestamp transformation
*
* @see https://momentjs.com/docs/#/displaying/format/
* @param format Format for timestamps
* Keep this transformation in production environments
*/
constructor(private readonly format = 'LLLL') {
// noop
}
useInProduction = true;
/**
* Add timestamp to output
@@ -37,8 +31,6 @@ export class Timestamp implements Transformation {
* @param output Output to add timestamp to
*/
transform(_logLevel: LogLevel, output: string): string {
const now = moment();
return `[${now.format(this.format)}] ${output}`;
return `[${new Date().toISOString()}] ${output}`;
}
}