UNPKG

1.98 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2018, Kinvey, Inc. All rights reserved.
3 *
4 * This software is licensed to you under the Kinvey terms of service located at
5 * http://www.kinvey.com/terms-of-use. By downloading, accessing and/or using this
6 * software, you hereby accept such terms of service (and any agreement referenced
7 * therein) and agree that you have read, understand and agree to be bound by such
8 * terms of service and are of legal age to agree to such terms with Kinvey.
9 *
10 * This software contains valuable confidential and proprietary information of
11 * KINVEY, INC and is subject to applicable licensing agreements.
12 * Unauthorized reproduction, transmission or distribution of this file and its
13 * contents is a violation of applicable laws.
14 */
15
16const winston = require('winston');
17
18const { LogLevel, StderrLogLevels } = require('./Constants');
19
20const levels = {
21 [LogLevel.ERROR]: 0,
22 [LogLevel.WARN]: 1,
23 [LogLevel.DATA]: 2,
24 [LogLevel.INFO]: 3,
25 [LogLevel.DEBUG]: 4
26};
27
28let logger = {};
29
30const formatter = (options) => {
31 const printMsgOnly = options.level === LogLevel.INFO || options.level === LogLevel.DATA;
32 if (printMsgOnly) {
33 return `${options.message}`;
34 }
35
36 const levelPart = logger.stripColors ? `[${options.level}]` : `[${winston.config.colorize(options.level)}]`;
37
38 if (options.level === LogLevel.ERROR) {
39 // if we have an error object it is retrieved from meta
40 const name = options.meta.name ? options.meta.name + ': ' : '';
41 const errMsg = options.message ? options.message : `${name}${options.meta.message}`;
42 const finalErrMsg = `${levelPart} ${errMsg}`;
43 return finalErrMsg;
44 }
45
46 return `${levelPart} ${options.message}`;
47};
48
49logger = new (winston.Logger)({
50 levels,
51 level: LogLevel.INFO,
52 stripColors: false,
53 transports: [
54 new (winston.transports.Console)({
55 formatter,
56 stderrLevels: StderrLogLevels
57 })
58 ]
59});
60
61module.exports = logger;