Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// TODO: Refactor options for logger (not for transport)
if (!loggers.has(name)) {
// add default Debug transport?
const defaultTransports = Object.keys(options || {}).length > 0 ? [] : [
new DebugTransport(options, name),
];
loggers.add(name, {
exitOnError: false,
level: 'info',
format: format.combine(
format.splat(), // formats level.message based on Node's util.format().
format.label({ label: name }),
format.colorize(),
format.timestamp(),
format.printf(({ level, message, label, timestamp }) => `${timestamp} [${label}] ${level}: ${message}`)
),
transports: defaultTransports,
});
loggers.get(name).on('error', (err: any) => {
if (err.code === 'ENOENT') {
let msg = err;
if (path.dirname(err.path) === '.') {
msg = 'Error from logger (winston) while trying to use a file to store logs:';
} else {
msg = 'Error from logger (winston) while trying to use a file to store logs, if the directory "'
+ err.path + '" does not exists please create it:';
}
// make the error intentionally more visible to get the attention of the user
console.error('------------------------');
console.error(msg, err);
function isLoggerType(str) {
return (str === 'info' || str === 'verbose' || str === 'debug');
}
/**
* The initial active logger attempts to read from environment variables.
* Otherwise, it defaults to 'info'.
*/
let activeLogger = process.env.TYR_LOG_LEVEL || 'info';
if (!isLoggerType(activeLogger)) {
activeLogger = 'info';
}
// Info level is only appended to the message if it's out of the ordinary (not 'info')
const customFormatting = format.printf((info) => {
const level = `[${info.level.toUpperCase()}] `;
return `${level}${info.message}`;
});
// Formatting for writing to a log file includes the timestamp and level
const logfileFormatting = format.printf((info) => {
const level = (`${info.level.toUpperCase()} `).slice(0, 7);
return `${info.timestamp} [${level}] ${info.message}`;
});
// Switches the order of debug and verbose so that verbose
// doesn't show during debug.
const customDebugLevels = {
levels: {
error: 0,
warn: 1,
private constructor() {
let dir = getLogDirectoryPath();
this.logger = createLogger({
level: "info",
format: format.combine(
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
format.printf(
info =>
`${info.timestamp} [${process.platform},${process.pid}] ${
info.level
}: ${info.message}`
)
),
transports: [
new transports.File({
filename: join(dir, "naotu.err.log"),
level: "error"
}),
new transports.File({
filename: join(dir, "naotu.log"),
maxsize: 104857600, // 100M
maxFiles: 50,
tailable: true
module: "",
...options,
};
this.winston = createLogger({
level: LogLevel[LogLevel.verbose], // log level switching handled in `createLogEntry`
defaultMeta: {
module: options.module || "",
},
transports: [
new transports.Console({
format: format.combine(
format.colorize(),
format.timestamp({
format: "YYYY-MM-DD HH:mm:ss"
}),
format.printf((info) => {
const paddingBetweenInfo = 30;
const infoString = (info.module || info.namespace || "");
const infoPad = paddingBetweenInfo - infoString.length;
return (
`${info.timestamp} [${infoString.toUpperCase()}] ${info.level.padStart(infoPad)}: ${info.message}`
);
})
),
handleExceptions: true
}),
],
exitOnError: false
});
//@ts-ignore
const logLevel = (): string => {
switch (NODE_ENV) {
case PRODUCTION: return "info";
case TEST: return "no_logging";
default: return "debug";
}
};
const logger: Logger = createLogger({
format: format.combine(
// Use these two instead for JSON format
// format.timestamp(),
// format.json()
format.timestamp({format: "YYYY-MM-DD HH:mm:ss.SSS"}),
format.printf((info): string => {
return `[${info.timestamp}] [${info.level.toUpperCase()}] ${info.message}`;
})
),
transports: [
new transports.Console({ level: logLevel() })
]
});
export default logger;
function _messageFormat() {
let template = conf.get(conf.keys.Logging.Template,
'%timestamp%%level%%label%%module%%message%%metadata%');
let timestampRegex = /%timestamp%/gi;
let levelRegex = /%level%/gi;
let labelRegex = /%label%/gi;
let moduleRegex = /%module%/gi;
let messageRegex = /%message%/gi;
let metadataRegex = /%metadata%/gi;
return format.printf(info => {
// NOTE: info will contain the mandatory "level" and "message" properties
// additionally it contains the "module" due to our wrapping approach
// plus it might contain the "timestamp" and "label" properties, if those formats are enabled
let output = template.replace(timestampRegex, info.timestamp || '');
output = output.replace(levelRegex, info.level || '');
output = output.replace(labelRegex, info.label || '');
output = output.replace(moduleRegex, info.module || '');
output = output.replace(messageRegex, info.message || '');
return output.replace(metadataRegex, info.metadata || '');
});
}
export default createLogger({
exitOnError: false,
transports: [
new WinstonToDebugLogger({
level: IS_DEBUG ? 'silly' : 'error'
}),
new transports.File({
name: '1',
filename: path.join(LOGS_FOLDER, 'web.log'),
level: IS_DEBUG ? 'silly' : 'info',
format: format.combine(
format.uncolorize(),
format.timestamp(),
format.padLevels(),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
),
maxsize: 5242880, // 5MB
maxFiles: -1,
eol: os.eol,
tailable: true
}),
new transports.File({
name: '2',
filename: path.join(LOGS_FOLDER, 'errors.log'),
format: format.simple(),
level: 'error'
})
],
exceptionHandlers: [
new transports.Console({
level: 'error'
public constructor(level: LogLevels = LogLevels.Debug, logfile?: string) {
this.winstonLogger = createLogger({
format: format.combine(
format.timestamp(),
format.printf(
info =>
`${info.timestamp} ${info.contextID} ${info.level}: ${info.message}`
)
),
level
});
if (logfile) {
this.fileTransport = new transports.File({ filename: logfile });
this.winstonLogger.add(this.fileTransport);
} else {
this.consoleTransport = new transports.Console();
this.winstonLogger.add(this.consoleTransport);
}
}
import {createLogger, format, transports} from 'winston'
export const logger = createLogger({
level: 'info',
format: format.combine(
format.colorize(),
format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
format.printf(info => {
return `${info.timestamp} ${info.level} ${info.message}`
}),
),
transports: [
new transports.Console(),
],
})
/*
Copyright (C) 2019 - ScuroGuardiano
This file is part of Dark Nova project.
This file and project is licensed under the MIT license
See file LICENSE in the root of this project or go to for full license details.
*/
import { createLogger, format, transports } from 'winston';
import config from '../config';
import * as colors from 'colors';
const consoleFormat = format.printf(data => {
const timestamp = colors.magenta(`${data.timestamp}`);
return `[${timestamp} ${data.level}]: ${data.message}`;
});
const fileFormat = format.printf(data => {
return `[${data.timestamp} ${data.level.toUpperCase()}]: ${data.message}`;
});
const logger = createLogger({
level: config.get('logging'),
transports: [
new transports.Console({
format: format.combine(
format.splat(),
format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
format.colorize(),