Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50675 views
1
/*
2
* console.js: Transport for outputting to the console
3
*
4
* (C) 2010 Charlie Robbins
5
* MIT LICENCE
6
*
7
*/
8
9
var events = require('events'),
10
util = require('util'),
11
colors = require('colors'),
12
common = require('../common'),
13
Transport = require('./transport').Transport;
14
15
//
16
// ### function Console (options)
17
// #### @options {Object} Options for this instance.
18
// Constructor function for the Console transport object responsible
19
// for persisting log messages and metadata to a terminal or TTY.
20
//
21
var Console = exports.Console = function (options) {
22
Transport.call(this, options);
23
options = options || {};
24
25
this.name = 'console';
26
this.json = options.json || false;
27
this.colorize = options.colorize || false;
28
this.prettyPrint = options.prettyPrint || false;
29
this.timestamp = typeof options.timestamp !== 'undefined' ? options.timestamp : false;
30
31
if (this.json) {
32
this.stringify = options.stringify || function (obj) {
33
return JSON.stringify(obj, null, 2);
34
};
35
}
36
};
37
38
//
39
// Inherit from `winston.Transport`.
40
//
41
util.inherits(Console, Transport);
42
43
//
44
// Expose the name of this Transport on the prototype
45
//
46
Console.prototype.name = 'console';
47
48
//
49
// ### function log (level, msg, [meta], callback)
50
// #### @level {string} Level at which to log the message.
51
// #### @msg {string} Message to log
52
// #### @meta {Object} **Optional** Additional metadata to attach
53
// #### @callback {function} Continuation to respond to when complete.
54
// Core logging method exposed to Winston. Metadata is optional.
55
//
56
Console.prototype.log = function (level, msg, meta, callback) {
57
if (this.silent) {
58
return callback(null, true);
59
}
60
61
var self = this,
62
output;
63
64
output = common.log({
65
colorize: this.colorize,
66
json: this.json,
67
level: level,
68
message: msg,
69
meta: meta,
70
stringify: this.stringify,
71
timestamp: this.timestamp,
72
prettyPrint: this.prettyPrint,
73
raw: this.raw
74
});
75
76
if (level === 'error' || level === 'debug') {
77
console.error(output);
78
} else {
79
console.log(output);
80
}
81
82
//
83
// Emit the `logged` event immediately because the event loop
84
// will not exit until `process.stdout` has drained anyway.
85
//
86
self.emit('logged');
87
callback(null, true);
88
};
89
90