mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-18 15:42:54 +00:00
@@ -12,14 +12,13 @@
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {Transport, VerifiableTransport} from './transport';
|
||||
|
||||
/**
|
||||
* A recursive partial object
|
||||
*
|
||||
* Copied from https://stackoverflow.com/a/51365037
|
||||
*/
|
||||
import {Transport, VerifiableTransport} from './transport';
|
||||
|
||||
export type RecursivePartial<T> = {
|
||||
[P in keyof T]?: T[P] extends Array<infer U> ?
|
||||
Array<RecursivePartial<U>> :
|
||||
|
||||
@@ -12,9 +12,10 @@
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import chalk from 'chalk';
|
||||
import {stringify} from 'flatted';
|
||||
import {isNodeEnvironment, isProductiveNodeEnvironment} from './common';
|
||||
import {Transformation} from './transformation';
|
||||
import {AddLogLevel} from './transformations/add-log-level';
|
||||
import {Transport} from './transport';
|
||||
|
||||
/**
|
||||
@@ -28,7 +29,12 @@ function hasStAppsLogLevel(something: object): something is { STAPPS_LOG_LEVEL:
|
||||
}
|
||||
|
||||
/**
|
||||
* Logger with colors, loglevel and transport
|
||||
* A log level
|
||||
*/
|
||||
export type LogLevel = 'INFO' | 'LOG' | 'WARN' | 'ERROR' | 'OK';
|
||||
|
||||
/**
|
||||
* A logger with transports and transformations
|
||||
*
|
||||
* Log level can be defined by setting the environment variable STAPPS_LOG_LEVEL to a valid log level. Log levels are
|
||||
* set in a binary way. For example STAPPS_LOG_LEVEL=12 does result in logs only for `Logger.warn` and `Logger.error`.
|
||||
@@ -51,7 +57,7 @@ export class Logger {
|
||||
/**
|
||||
* Log levels
|
||||
*/
|
||||
private static readonly logLevels = [
|
||||
private static readonly logLevels: LogLevel[] = [
|
||||
'INFO',
|
||||
'LOG',
|
||||
'WARN',
|
||||
@@ -64,17 +70,43 @@ export class Logger {
|
||||
*/
|
||||
private static readonly logLevelSum = Math.pow(Logger.binaryBase, Logger.logLevels.length) - 1;
|
||||
|
||||
/**
|
||||
* Transformers for log output
|
||||
*/
|
||||
private static transformations?: Transformation[] = [
|
||||
new AddLogLevel(),
|
||||
];
|
||||
|
||||
/**
|
||||
* Transport for errors
|
||||
*/
|
||||
private static transport?: Transport;
|
||||
|
||||
/**
|
||||
* Apply transformations to an output
|
||||
*
|
||||
* @param logLevel Log level of the output
|
||||
* @param output Output to apply transformations to
|
||||
*/
|
||||
private static applyTransformers(logLevel: LogLevel, output: string): string {
|
||||
if (!Array.isArray(Logger.transformations) || Logger.transformations.length === 0) {
|
||||
return output;
|
||||
}
|
||||
|
||||
let transformedOutput = output;
|
||||
for (const transformation of Logger.transformations) {
|
||||
transformedOutput = transformation.transform(logLevel, transformedOutput);
|
||||
}
|
||||
|
||||
return transformedOutput;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if intended log level is allowed in environment log level
|
||||
*
|
||||
* @param logLevel Log level to check
|
||||
*/
|
||||
private static checkLogLevel(logLevel: string): boolean {
|
||||
private static checkLogLevel(logLevel: LogLevel): boolean {
|
||||
const logLevelNumber = Math.pow(Logger.binaryBase, Logger.logLevels.indexOf(logLevel) + 1) - 1;
|
||||
|
||||
// tslint:disable-next-line:no-bitwise
|
||||
@@ -110,7 +142,7 @@ export class Logger {
|
||||
}
|
||||
|
||||
/* tslint:disable-next-line:no-console */
|
||||
console.error(chalk.bold.red(`[ERROR] ${Logger.stringifyArguments(...args)}`));
|
||||
console.error(Logger.applyTransformers('ERROR', Logger.stringifyArguments(...args)));
|
||||
|
||||
if (isProductiveNodeEnvironment()) {
|
||||
if (typeof Logger.transport !== 'undefined') {
|
||||
@@ -134,7 +166,7 @@ export class Logger {
|
||||
}
|
||||
|
||||
/* tslint:disable-next-line:no-console */
|
||||
console.info(chalk.cyan(`[INFO] ${Logger.stringifyArguments(...args)}`));
|
||||
console.info(Logger.applyTransformers('INFO', Logger.stringifyArguments(...args)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -146,8 +178,7 @@ export class Logger {
|
||||
throw new Error(`Productive environment doesn't set a transport for error notifications.`);
|
||||
}
|
||||
|
||||
/* tslint:disable-next-line:no-console */
|
||||
console.warn(chalk.yellow(`Productive environment doesn't set a transport for error notifications.`));
|
||||
Logger.warn(`Productive environment doesn't set a transport for error notifications.`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,7 +193,7 @@ export class Logger {
|
||||
}
|
||||
|
||||
/* tslint:disable-next-line:no-console */
|
||||
console.log(chalk.white(`[LOG] ${Logger.stringifyArguments(...args)}`));
|
||||
console.log(Logger.applyTransformers('LOG', Logger.stringifyArguments(...args)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,7 +207,16 @@ export class Logger {
|
||||
}
|
||||
|
||||
/* tslint:disable-next-line:no-console */
|
||||
console.log(chalk.bold.green(`[OK] ${Logger.stringifyArguments(...args)}`));
|
||||
console.log(Logger.applyTransformers('OK', Logger.stringifyArguments(...args)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set transformations for log output
|
||||
*
|
||||
* @param transformations List of transformations
|
||||
*/
|
||||
public static setTransformations(transformations: Transformation[]) {
|
||||
Logger.transformations = transformations;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -224,6 +264,6 @@ export class Logger {
|
||||
}
|
||||
|
||||
/* tslint:disable-next-line:no-console */
|
||||
console.warn(chalk.yellow(`[WARN] ${Logger.stringifyArguments(...args)}`));
|
||||
console.warn(Logger.applyTransformers('WARN', Logger.stringifyArguments(...args)));
|
||||
}
|
||||
}
|
||||
|
||||
28
src/transformation.ts
Normal file
28
src/transformation.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {LogLevel} from './logger';
|
||||
|
||||
/**
|
||||
* A transformer for log output
|
||||
*/
|
||||
export interface Transformation {
|
||||
/**
|
||||
* Transform an output
|
||||
*
|
||||
* @param logLevel Log level to transform output for
|
||||
* @param output Output to transform
|
||||
*/
|
||||
transform(logLevel: LogLevel, output: string): string;
|
||||
}
|
||||
32
src/transformations/add-log-level.ts
Normal file
32
src/transformations/add-log-level.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import {LogLevel} from '../logger';
|
||||
import {Transformation} from '../transformation';
|
||||
|
||||
/**
|
||||
* Transformation that adds the log level to output
|
||||
*/
|
||||
export class AddLogLevel implements Transformation {
|
||||
/**
|
||||
* Add log level to output
|
||||
*
|
||||
* @param logLevel Log level to add to output
|
||||
* @param output Output to colorize
|
||||
*/
|
||||
// tslint:disable-next-line:prefer-function-over-method
|
||||
transform(logLevel: LogLevel, output: string): string {
|
||||
return `[${logLevel}] ${output}`;
|
||||
}
|
||||
}
|
||||
47
src/transformations/colorize.ts
Normal file
47
src/transformations/colorize.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import chalk, {Chalk} from 'chalk';
|
||||
import {LogLevel} from '../logger';
|
||||
import {Transformation} from '../transformation';
|
||||
|
||||
/**
|
||||
* Transformation that colorizes log output
|
||||
*/
|
||||
export class Colorize implements Transformation {
|
||||
/**
|
||||
* Instantiate a new colorize transformation
|
||||
*
|
||||
* @param logLevelToColor Map from log level to color transformation to apply
|
||||
*/
|
||||
constructor(private readonly logLevelToColor: { [k in LogLevel]: Chalk; } = {
|
||||
ERROR: chalk.bold.red,
|
||||
INFO: chalk.cyan,
|
||||
LOG: chalk.white,
|
||||
OK: chalk.bold.green,
|
||||
WARN: chalk.yellow,
|
||||
}) {
|
||||
// noop
|
||||
}
|
||||
|
||||
/**
|
||||
* Colorize log output
|
||||
*
|
||||
* @param logLevel Log level to choose color for
|
||||
* @param output Output to colorize
|
||||
*/
|
||||
transform(logLevel: LogLevel, output: string): string {
|
||||
return this.logLevelToColor[logLevel](output);
|
||||
}
|
||||
}
|
||||
45
src/transformations/timestamp.ts
Normal file
45
src/transformations/timestamp.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import * as moment from 'moment';
|
||||
import {LogLevel} from '../logger';
|
||||
import {Transformation} from '../transformation';
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
constructor(private readonly format = 'LLLL') {
|
||||
// noop
|
||||
}
|
||||
|
||||
/**
|
||||
* Add timestamp to output
|
||||
*
|
||||
* @param _logLevel Log level to add timestamp to output for
|
||||
* @param output Output to add timestamp to
|
||||
*/
|
||||
transform(_logLevel: LogLevel, output: string): string {
|
||||
const now = moment();
|
||||
|
||||
return `[${now.format(this.format)}] ${output}`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user