blob: 8f46acb915ef67429008176c990e1b6ba9c2963d [file] [log] [blame]
Matteo Scandolobd13aab2017-08-08 13:05:24 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080019(function () {
20 'use strict';
21
22 const winston = require('winston');
23 const fs = require('fs');
24 const path = require('path');
Matteo Scandolof05d8a62016-12-06 13:36:49 -080025 const level = process.env.LOG_LEVEL || 'debug';
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080026 winston.level = level;
27
28 const logFile = path.join(__dirname, '../../logs/xos-nb-rest');
29
30 // clear old logs
31 ['error', 'debug'].forEach(l => {
32 try {
33 fs.statSync(`${logFile}.${l}.log`)
34 fs.unlinkSync(`${logFile}.${l}.log`);
35 }
36 catch(e) {
37 // log does not exist
38 }
39 });
40
41 // create a custom logger with colorized console and persistance to file
42 const logger = new (winston.Logger)({
43 transports: [
44 new (winston.transports.Console)({level: level, colorize: true}),
45 new (winston.transports.File)({name: 'error-log', level: 'error', filename: `${logFile}.error.log`}),
46 new (winston.transports.File)({name: 'debug-log', level: 'debug', filename: `${logFile}.debug.log`})
47 ]
48 });
49
50 module.exports = logger;
51
52})();