mirror of
https://github.com/strapi/strapi.git
synced 2025-07-18 22:45:47 +00:00
38 lines
1002 B
JavaScript
38 lines
1002 B
JavaScript
![]() |
'use strict';
|
||
|
|
||
|
const { format } = require('winston');
|
||
|
const { isString } = require('lodash/fp');
|
||
|
|
||
|
const { combine, timestamp, colorize, printf } = format;
|
||
|
|
||
|
const defaultTimestampFormat = 'YYYY-MM-DD HH:mm:ss.SSS';
|
||
|
|
||
|
/**
|
||
|
* Create a pretty print formatter for a winston logger
|
||
|
* @param {string|boolean} timestamps - Enable or disable timestamps print if it's a boolean value. Use the given format for the timestamps if it's a string
|
||
|
* @param {boolean} colors - Enable or disable the use of colors for the log level
|
||
|
*/
|
||
|
module.exports = ({ timestamps = true, colors = true } = {}) => {
|
||
|
const handlers = [];
|
||
|
|
||
|
if (timestamps) {
|
||
|
handlers.push(
|
||
|
timestamp({
|
||
|
format: isString(timestamps) ? timestamps : defaultTimestampFormat,
|
||
|
})
|
||
|
);
|
||
|
}
|
||
|
|
||
|
if (colors) {
|
||
|
handlers.push(colorize());
|
||
|
}
|
||
|
|
||
|
handlers.push(
|
||
|
printf(({ level, message, timestamp }) => {
|
||
|
return `${timestamps ? `[${timestamp}] ` : ''}${level}: ${message}`;
|
||
|
})
|
||
|
);
|
||
|
|
||
|
return combine(...handlers);
|
||
|
};
|