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 |
|
---|
7 | let log4js = require('log4js')
|
---|
8 | const helper = require('./helper')
|
---|
9 | const 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.
|
---|
24 | function 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.
|
---|
72 | function 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 |
|
---|
86 | const 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.
|
---|
93 | function 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 |
|
---|
110 | exports.create = create
|
---|
111 | exports.setup = setup
|
---|
112 | exports.setupFromConfig = setupFromConfig
|
---|
113 | exports._rebindLog4js4testing = function (mockLog4js) {
|
---|
114 | log4js = mockLog4js
|
---|
115 | }
|
---|