mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 00:23:03 +00:00
refactor: move logger to monorepo
This commit is contained in:
37
packages/logger/src/transformations/add-log-level.ts
Normal file
37
packages/logger/src/transformations/add-log-level.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* 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 {
|
||||
/**
|
||||
* Keep this transformation in production environments
|
||||
*/
|
||||
useInProduction = true;
|
||||
|
||||
/**
|
||||
* 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}`;
|
||||
}
|
||||
}
|
||||
54
packages/logger/src/transformations/colorize.ts
Normal file
54
packages/logger/src/transformations/colorize.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (C) 2019-2020 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 from 'chalk';
|
||||
import {LogLevel} from '../logger';
|
||||
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
|
||||
*
|
||||
* @param logLevelToColor Map from log level to color transformation to apply
|
||||
*/
|
||||
constructor(
|
||||
private readonly logLevelToColor: {[k in LogLevel]: chalk.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);
|
||||
}
|
||||
}
|
||||
36
packages/logger/src/transformations/timestamp.ts
Normal file
36
packages/logger/src/transformations/timestamp.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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 a timestamp to output
|
||||
*/
|
||||
export class Timestamp implements Transformation {
|
||||
/**
|
||||
* Keep this transformation in production environments
|
||||
*/
|
||||
useInProduction = true;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
return `[${new Date().toISOString()}] ${output}`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user