source: trip-planner-front/node_modules/karma/lib/logger.js@ 59329aa

Last change on this file since 59329aa was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.5 KB
Line 
1// This is the **logger** module for *Karma*. It uses
2// [log4js](https://github.com/nomiddlename/log4js-node) to handle and
3// configure all logging that happens inside of *Karma*.
4
5// ### Helpers and Setup
6
7let log4js = require('log4js')
8const helper = require('./helper')
9const constant = require('./constants')
10
11// #### Public Functions
12
13// Setup the logger by passing in the configuration options. It needs
14// three arguments:
15//
16// setup(logLevel, colors, appenders)
17//
18// * `logLevel`: *String* Defines the global log level.
19// * `colors`: *Boolean* Use colors in the stdout or not.
20// * `appenders`: *Object* This will be passed as appenders to log4js
21// to allow for fine grained configuration of log4js. For more information
22// see https://github.com/nomiddlename/log4js-node.
23// *Array* is also accepted for backwards compatibility.
24function setup (level, colors, appenders) {
25 // Turn color on/off on the console appenders with pattern layout
26 const pattern = colors ? constant.COLOR_PATTERN : constant.NO_COLOR_PATTERN
27 if (appenders) {
28 // Convert Array to Object for backwards compatibility.
29 if (appenders.map) {
30 if (appenders.length === 0) {
31 appenders = [constant.CONSOLE_APPENDER]
32 }
33 const v1Appenders = appenders
34 appenders = {}
35 v1Appenders.forEach(function (appender, index) {
36 if (appender.type === 'console') {
37 appenders.console = appender
38 if (helper.isDefined(appender.layout) && appender.layout.type === 'pattern') {
39 appender.layout.pattern = pattern
40 }
41 } else {
42 appenders[index + ''] = appender
43 }
44 return appender
45 })
46 }
47 } else {
48 appenders = { console: constant.CONSOLE_APPENDER }
49 }
50
51 log4js.configure({
52 appenders: appenders,
53 categories: {
54 default: {
55 appenders: Object.keys(appenders),
56 level: level
57 }
58 }
59 })
60}
61
62// Setup the logger by passing in the config object. The function sets the
63// `colors` and `logLevel` if they are defined. It takes two arguments:
64//
65// setupFromConfig(config, appenders)
66//
67// * `config`: *Object* The configuration object.
68// * `appenders`: *Object* This will be passed as appenders to log4js
69// to allow for fine grained configuration of log4js. For more information
70// see https://github.com/nomiddlename/log4js-node.
71// *Array* is also accepted for backwards compatibility.
72function setupFromConfig (config, appenders) {
73 let useColors = true
74 let logLevel = constant.LOG_INFO
75
76 if (helper.isDefined(config.colors)) {
77 useColors = config.colors
78 }
79
80 if (helper.isDefined(config.logLevel)) {
81 logLevel = config.logLevel
82 }
83 setup(logLevel, useColors, appenders)
84}
85
86const loggerCache = {}
87
88// Create a new logger. There are two optional arguments
89// * `name`, which defaults to `karma` and
90// If the `name = 'socket.io'` this will create a special wrapper
91// to be used as a logger for socket.io.
92// * `level`, which defaults to the global level.
93function create (name, level) {
94 name = name || 'karma'
95 let logger
96 if (Object.prototype.hasOwnProperty.call(loggerCache, name)) {
97 logger = loggerCache[name]
98 } else {
99 logger = log4js.getLogger(name)
100 loggerCache[name] = logger
101 }
102 if (helper.isDefined(level)) {
103 logger.setLevel(level)
104 }
105 return logger
106}
107
108// #### Publish
109
110exports.create = create
111exports.setup = setup
112exports.setupFromConfig = setupFromConfig
113exports._rebindLog4js4testing = function (mockLog4js) {
114 log4js = mockLog4js
115}
Note: See TracBrowser for help on using the repository browser.