UNPKG

3.66 kBJavaScriptView Raw
1/**
2 * @class AbstractAppender
3 *
4 * @author: darryl.west@raincitysoftware.com
5 * @created: 7/7/14 5:58 PM
6 */
7const util = require( 'util' );
8const moment = require( 'moment' );
9const dash = require( 'lodash' );
10
11const AbstractAppender = function(options) {
12 'use strict';
13
14 const appender = this;
15 const typeName = options.typeName;
16 const timestampFormat = options.timestampFormat || 'HH:mm:ss.SSS';
17
18 this.separator = options.separator || ' ';
19
20 /**
21 * format the entry and return the field list
22 *
23 * @param entry the log entry
24 * @param thisArg - use this to override the base object
25 *
26 * @returns field array
27 */
28 this.formatEntry = function(entry, thisArg) {
29 const apdr = thisArg || appender;
30
31 const fields = [];
32
33 if (entry.domain) {
34 fields.push( entry.domain );
35 }
36
37 fields.push( apdr.formatTimestamp( entry.ts ) );
38 fields.push( apdr.formatLevel( entry.level ) );
39
40 if (entry.category) {
41 fields.push( entry.category );
42 }
43
44 fields.push( apdr.formatMessage( entry.msg ) );
45
46 return fields;
47 };
48
49 /**
50 * format the message
51 *
52 * @param msg the log message
53 * @param thisArg - use this to override the base object
54 *
55 * @returns field array
56 */
57 this.formatMessage = function(msg, thisArg) {
58 const apdr = thisArg || appender;
59
60 if (!msg) {
61 return '';
62 }
63
64 if (util.isArray( msg )) {
65 const list = msg.map(function(item) {
66 if (util.isDate( item )) {
67 return apdr.formatDate( item );
68 } else {
69 return apdr.formatObject( item );
70 }
71 });
72
73 return list.join('');
74 } else {
75 return msg;
76 }
77 };
78
79 this.formatDate = function(value) {
80 return value.toJSON();
81 };
82
83 this.formatObject = function(value) {
84 if (!value) {
85 return '';
86 }
87
88 if (dash.isObject( value )) {
89 if (value instanceof Error) {
90 return [ value.message, value.stack ].join('\n');
91 }
92 try {
93 return JSON.stringify( value );
94 } catch (ignore) {
95 return 'json error: ' + value.toString();
96 }
97 } else {
98 var s = value.toString();
99 if (s === '[object Object]') {
100 return util.inspect( value );
101 } else {
102 return s;
103 }
104 }
105 };
106
107 /**
108 * format the level string by forcing to upper case and padding to 5 chars
109 *
110 * @param level
111 * @returns {string}
112 */
113 this.formatLevel = function(level) {
114 let str = level.toUpperCase();
115 if (str.length < 5) {
116 str += ' ';
117 }
118
119 return str;
120 };
121
122 /**
123 * format the timestamp to HH:mm:ss.SSS
124 *
125 * @param ts the unix milliseconds
126 * @returns formatted string
127 */
128 this.formatTimestamp = function(ts) {
129 return moment( ts ).format( timestampFormat );
130 };
131
132 /**
133 * return the type name of this appender (ConsoleAppender)
134 */
135 this.getTypeName = function() {
136 return typeName;
137 };
138
139 // constructor tests
140 if (!typeName) {
141 throw new Error('appender must be constructed with a type name');
142 }
143};
144
145module.exports = AbstractAppender;
146
147AbstractAppender.extend = function(child, options) {
148 'use strict';
149
150 const parent = new AbstractAppender( options );
151
152 dash.extend( child, parent );
153
154 return parent;
155};