tachybase_todo/packages/core/logger/src/create-logger.ts
ChengLei Shao 3263a241ab
feat: logging package (#1021)
* feat: logging package

* chore: dependency

* chore: pino

* feat: request log

* feat: log rotate

* fix: gitignore

* fix: gitignore

* fix: middleware await

* fix: test

* docs: logging

* feat: log error handler errors

* feat: improve code

* fix: response error log level

* feat: log http body

* feat: improve code

* feat: improve code

* feat: update docs

* fix: error

* feat: improve code

* fix: deps

* feat: update docs

* fix: error

* fix:  colorize

Co-authored-by: chenos <chenlinxh@gmail.com>
2022-11-12 17:12:50 +08:00

68 lines
1.8 KiB
TypeScript

import path from 'path';
import winston, { format, Logger } from 'winston';
import 'winston-daily-rotate-file';
const { combine, timestamp, colorize, simple } = format;
function loggingLevel() {
return process.env.LOGGER_LEVEL || 'info';
}
const Transports = {
console(options) {
return new winston.transports.Console({
format: combine(simple(), colorize()),
...options,
});
},
dailyRotateFile(options: any) {
let dirname = process.env.DAILY_ROTATE_FILE_DIRNAME || path.resolve(process.cwd(), './storage/logs');
if (!path.isAbsolute(dirname)) {
dirname = path.resolve(process.cwd(), dirname);
}
return new winston.transports.DailyRotateFile({
dirname,
level: loggingLevel(),
filename: 'nocobase-%DATE%.log',
datePattern: 'YYYY-MM-DD-HH',
maxFiles: '14d',
...options,
});
},
};
function toArr(value: any) {
if (Array.isArray(value)) {
return value;
}
return value ? value.split(',') : [];
}
type WinstonTransport = string | string[] | winston.transport | winston.transport[];
interface LoggerOptions extends Omit<winston.LoggerOptions, 'transports'> {
transports?: WinstonTransport;
}
function createLogger(options: LoggerOptions = {}) {
const transports: winston.transport[] = toArr(options?.transports || ['console', 'dailyRotateFile'])
.map((t) => {
if (typeof t === 'string') {
return Transports[t]();
}
return t;
})
.filter((t) => t);
const logger = winston.createLogger({
level: loggingLevel(),
levels: winston.config.cli.levels,
format: combine(timestamp(), format.errors({ stack: true }), format.json(), colorize()),
...options,
transports,
});
winston.addColors(winston.config.cli.colors);
return logger;
}
export { Logger, LoggerOptions, Transports, createLogger };