source: trip-planner-front/node_modules/log4js/lib/LoggingEvent.js@ fa375fe

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

initial commit

  • Property mode set to 100644
File size: 2.2 KB
Line 
1const flatted = require('flatted');
2const levels = require('./levels');
3
4/**
5 * @name LoggingEvent
6 * @namespace Log4js
7 */
8class LoggingEvent {
9 /**
10 * Models a logging event.
11 * @constructor
12 * @param {string} categoryName name of category
13 * @param {Log4js.Level} level level of message
14 * @param {Array} data objects to log
15 * @author Seth Chisamore
16 */
17 constructor(categoryName, level, data, context, location) {
18 this.startTime = new Date();
19 this.categoryName = categoryName;
20 this.data = data;
21 this.level = level;
22 this.context = Object.assign({}, context);
23 this.pid = process.pid;
24
25 if (location) {
26 this.functionName = location.functionName;
27 this.fileName = location.fileName;
28 this.lineNumber = location.lineNumber;
29 this.columnNumber = location.columnNumber;
30 this.callStack = location.callStack;
31 }
32 }
33
34 serialise() {
35 const logData = this.data.map((e) => {
36 // JSON.stringify(new Error('test')) returns {}, which is not really useful for us.
37 // The following allows us to serialize errors correctly.
38 if (e && e.message && e.stack) {
39 e = Object.assign({ message: e.message, stack: e.stack }, e);
40 }
41 return e;
42 });
43 this.data = logData;
44 return flatted.stringify(this);
45 }
46
47 static deserialise(serialised) {
48 let event;
49 try {
50 const rehydratedEvent = flatted.parse(serialised);
51 rehydratedEvent.data = rehydratedEvent.data.map((e) => {
52 if (e && e.message && e.stack) {
53 const fakeError = new Error(e);
54 Object.keys(e).forEach((key) => { fakeError[key] = e[key]; });
55 e = fakeError;
56 }
57 return e;
58 });
59 event = new LoggingEvent(
60 rehydratedEvent.categoryName,
61 levels.getLevel(rehydratedEvent.level.levelStr),
62 rehydratedEvent.data,
63 rehydratedEvent.context
64 );
65 event.startTime = new Date(rehydratedEvent.startTime);
66 event.pid = rehydratedEvent.pid;
67 event.cluster = rehydratedEvent.cluster;
68 } catch (e) {
69 event = new LoggingEvent(
70 'log4js',
71 levels.ERROR,
72 ['Unable to parse log:', serialised, 'because: ', e]
73 );
74 }
75
76 return event;
77 }
78}
79
80module.exports = LoggingEvent;
Note: See TracBrowser for help on using the repository browser.