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 | var log4js = require('log4js')
|
8 | var helper = require('./helper')
|
9 | var 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`: *Array* 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 | var setup = function (level, colors, appenders) {
|
24 | // Turn color on/off on the console appenders with pattern layout
|
25 | var pattern = colors ? constant.COLOR_PATTERN : constant.NO_COLOR_PATTERN
|
26 |
|
27 | // If there are no appenders use the default one
|
28 | appenders = helper.isDefined(appenders) ? appenders : [constant.CONSOLE_APPENDER]
|
29 |
|
30 | appenders = appenders.map(function (appender) {
|
31 | if (appender.type === 'console') {
|
32 | if (helper.isDefined(appender.layout) && appender.layout.type === 'pattern') {
|
33 | appender.layout.pattern = pattern
|
34 | }
|
35 | }
|
36 | return appender
|
37 | })
|
38 |
|
39 | // Pass the values to log4js
|
40 | log4js.setGlobalLogLevel(level)
|
41 | log4js.configure({
|
42 | appenders: appenders
|
43 | })
|
44 | }
|
45 |
|
46 | // Create a new logger. There are two optional arguments
|
47 | // * `name`, which defaults to `karma` and
|
48 | // If the `name = 'socket.io'` this will create a special wrapper
|
49 | // to be used as a logger for socket.io.
|
50 | // * `level`, which defaults to the global level.
|
51 | var create = function (name, level) {
|
52 | var logger = log4js.getLogger(name || 'karma')
|
53 | if (helper.isDefined(level)) {
|
54 | logger.setLevel(level)
|
55 | }
|
56 | return logger
|
57 | }
|
58 |
|
59 | // #### Publish
|
60 |
|
61 | exports.create = create
|
62 | exports.setup = setup
|