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