UNPKG

@telefonica/opensource-scaffold

Version:

Scaffolding for open source projects. A CLI tool to create open source repositories with standard tools and resources

44 lines (43 loc) 1.48 kB
// SPDX-FileCopyrightText: 2024 Telefónica Innovación Digital // SPDX-License-Identifier: Apache-2.0 import chalk from "chalk"; import winston from "winston"; const { combine, colorize, timestamp, printf } = winston.format; /** * The supported log levels */ export const LOG_LEVELS = ["silly", "debug", "verbose", "info", "warning", "error"]; /** * The default log level */ export const DEFAULT_LEVEL = "info"; /** * Print extra information to the log. Any object passed as a second argument to the winston logger will be printed as a gray json string due to this function. * @param info The log information * @returns A json string with the extra information. */ function printExtraInfo(info) { const objectToPrint = {}; for (const key in info) { if (key !== "message" && key !== "level" && key !== "timestamp") { objectToPrint[key] = info[key]; } } if (Object.keys(objectToPrint).length === 0) { return ""; } return `: ${chalk.gray(JSON.stringify(objectToPrint))}`; } /** * Returns a winston logger * @param level The log level * @returns Winston logger */ export function createLogger(level) { const logger = winston.createLogger({ level: level || DEFAULT_LEVEL, format: combine(colorize(), timestamp(), printf((info) => `${info.timestamp} ${info.level}: ${info.message}${printExtraInfo(info)}`)), transports: [new winston.transports.Console()], }); return logger; }