File size: 2,578 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/**
 * Credit: Modified from `winston-namespace` by @SetaSouto:
 * 	https://github.com/SetaSouto/winston-namespace
 *
 * Key changes:
 *
 * 1. Use a custom base configuration to avoid overriding the default configuration when
 * initializing each logger object. This maintains the same logger initialization API
 * as the prior logging solution (`debug` module).
 *
 * 2. Maintain prior log filtering behavior with the `DEBUG` environment variable
 * (instead of the `LOG_NAMESPACES` variable used in winston-namespace).
 */

const path = require( 'path' );
const { createLogger, format, transports } = require( 'winston' );
const state = require( '../../lib/state' );
const namespaces = require( './namespaces' );

/**
 * Module variables
 */
const maxFiles = 3;
const maxsize = 15000000;

module.exports = ( namespace, options ) => {
	if ( ! options || typeof options !== 'object' ) {
		options = {};
	}

	const formatMessageWithMeta = ( info ) => {
		const args = info[ Symbol.for( 'splat' ) ];
		if ( args ) {
			if ( args instanceof Array && args.length && ! args[ 0 ] ) {
				return info;
			}
			info.message = info.message + JSON.stringify( ...args );
		}
		return info;
	};

	const baseformat = format.combine(
		format.timestamp( {
			format: 'YYYY-MM-DD HH:mm:ss.SSS',
		} ),
		format.errors( { stack: true } ),
		format( ( info, opts ) => formatMessageWithMeta( info, opts ) )(),
		format.printf( ( info ) => {
			const { timestamp, level, message } = info;
			const stack = info.stack ? `\n${ info.stack }` : '';
			return `[${ timestamp }] [${ namespace }] [${ level }] ${ message }` + stack;
		} )
	);

	const baseOptions = {
		level: process.env.LOG_LEVEL || 'silly',
		transports: [
			new transports.File( {
				dirname: path.dirname( state.getLogPath() ),
				filename: path.basename( state.getLogPath() ),
				maxFiles,
				maxsize,
				format: baseformat,
			} ),
		],
	};

	const enabled = namespaces.check( namespace );
	const logger = createLogger( { ...baseOptions, ...options } );
	if ( process.env.DEBUG ) {
		logger.add(
			new transports.Console( {
				format: baseformat,
			} )
		);
	}

	return {
		error: ( message, meta ) => logger.error( message, meta ),
		warn: ( message, meta ) => logger.warn( message, meta ),
		info: ( message, meta ) => logger.info( message, meta ),
		debug: ( message, meta ) => {
			if ( enabled ) {
				logger.debug( message, meta );
			}
		}, // eslint-disable-line brace-style
		silly: ( message, meta ) => {
			if ( enabled ) {
				logger.silly( message, meta );
			}
		}, // eslint-disable-line brace-style
	};
};