id0sch / log4js-json-layout Goto Github PK
View Code? Open in Web Editor NEWprovides a slim and easy to use json-layout for log4js-node
License: MIT License
provides a slim and easy to use json-layout for log4js-node
License: MIT License
I must be missing something. I can only include startTime
, categoryName
, level
, and data
in my json output.
It appears to be because the formatter code selects the fields from output
instead of data
:
// Only include fields specified in 'include' field
// if field is specified
if (config.include && config.include.length) {
const newOutput = {};
_.forEach(config.include, (key) => {
if (_.has(output, key)) {
newOutput[key] = output[key];
}
});
return newOutput;
}
data
is set earlier as a clone of the formatter input:
function formatter(raw) {
const data = _.clone(raw);
...
and output specifically only selects a few fields:
const output = {
startTime: data.startTime,
categoryName: data.categoryName,
level: data.level.levelStr,
};
It would appear that this bit of code at line 105:
if (_.has(output, key)) {
newOutput[key] = output[key];
}
should be:
if (_.has(data, key)) {
newOutput[key] = data[key];
}
Am I missing something? I assume I'm doing something wrong...
I also noticed error objects are spitting out incorrect messages.
This is evident just by running mocha:
1) log4js-json-layout should format stack traces:
AssertionError: expected '{"startTime":"615 Ludlam Place, Nicholson, New Mexico, 5763","categoryName":"572efdaaa64be9dbc56369ae","level":"INFO","data":"{ inspect: [Function: inspect] }"}' to include '"data":"Error: Whoops!\\n at Context.<anonymous>'
at Context.<anonymous> (test/unit/lib/jsonLayout.js:68:19)
at processImmediate (internal/timers.js:461:21)
The data field is supposed to be the stack trace, but instead it's coming out"data":"{ inspect: [Function: inspect] }"
.
It would appear something has changed between node and maybe lodash to cause this.
I don't quite get how this is supposed to work. I'd be happy to put together a PR to fix this, but I need to determine root cause first.
Can someone explain how inspect()
is supposed to work?
function wrapErrorsWithInspect(items) {
return _(items).map((item) => {
if (_.isError(item) && item.stack) {
return {
inspect() {
return `${util.format(item)}\n${item.stack}`;
},
};
} else if (!_.isObject(item)) {
return item;
}
return undefined;
}).compact().value();
}
function formatLogData(data) {
return util.format(...wrapErrorsWithInspect(data));
}
I would like to change the layout from:
{"startTime":"2019-02-28T04:27:24.363Z","categoryName":"app","level":"INFO","data":"Test log 1000000000"}
to
{"startTime":"2019-02-28T04:27:24.363Z","categoryName":"app","level":"INFO","message":"Test log 1000000000"}
Can you guide me how to do it ?
for better compatibility with some platforms and with a java version it would be nice to change the name of some fields like:
starttime to time ...
Hello,
To vet something more conformtable I use log4js with a configuration file like:
{
"appenders": {
"services" : {
"type": "dateFile",
"filename": "../logs/cld-apps-services.log",
"pattern": "-yyyy-MM-dd",
"maxLogSize": 20480,
"backups": 3
},
"billing": {
"type": "dateFile",
"filename": "../logs/cld-apps-billing.log",
"pattern": "-yyyy-MM-dd",
"maxLogSize": 20480,
"backups": 10
},
"accessLog": {
"type": "dateFile",
"filename": "../logs/cld-apps-access-log.log",
"pattern": "-yyyy-MM-dd",
"maxLogSize": 20480,
"backups": 3
},
"errorFile": {
"type": "file",
"filename": "../logs/cld-apps-errors.log",
"layout": {
"type": "messagePassThrough"
}
},
"errors": {
"type": "logLevelFilter",
"level": "ERROR",
"appender": "errorFile"
},
"testu": {
"type": "dateFile",
"filename": "../logs/testu.log",
"pattern": "-yyyy-MM-dd",
"maxLogSize": 20480,
"backups": 3
},
"console": {
"type": "console"
},
"stdout": {
type: "stdout"
},
"cloudstdout": {
"type": "logLevelFilter",
"level": "DEBUG",
"appender": "stdout"
},
"stderr": {
type: "stderr"
},
"cloudstdout": {
"type": "logLevelFilter",
"level": "ERROR",
"appender": "stderr"
}
},
"categories": {
"default": { "appenders": ["console", "cloudstdout", "cloudstderr"], "level": "INFO" },
"testu": { "appenders": ["testu", "services"], "level": "TRACE" },
"testi": { "appenders": ["testu", "services"], "level": "TRACE" },
"jmcnet": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "INFO" },
"jmcnet.emailTemplate": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "DEBUG" },
"ppnjs": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "INFO" },
"access-log": { "appenders": ["services", "accessLog", "cloudstdout", "cloudstderr" ], "level": "INFO" },
"cld-apps": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "INFO" },
"cld-apps.billing": { "appenders": ["services", "errors", "billing", "cloudstdout", "cloudstderr" ], "level": "INFO" },
"cld-apps.models": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "INFO" },
"cld-apps.models.baseSchema": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "WARN" },
"cld-apps.models.accounts": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "INFO" },
"cld-apps.models.mailpoeteAccount": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "INFO" },
"cld-apps.models.spaces": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "DEBUG" },
"cld-apps.models.files": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "DEBUG" },
"cld-apps.models.fileSpaces": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "DEBUG" },
"cld-apps.utils": { "appenders": ["services", "errors" ], "level": "INFO", "cloudstdout", "cloudstderr" },
"cld-apps.utils.helperAws": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "DEBUG" },
"cld-apps.utils.helperZip": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "DEBUG" },
"cld-apps.services": { "appenders": ["services", "errors" ], "level": "INFO", "cloudstdout", "cloudstderr" },
"cld-apps.services.periodics": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "INFO" },
"cld-apps.services.accounts": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "INFO" },
"cld-apps.services.spaces": { "appenders": ["services", "errors", "cloudstdout", "cloudstderr" ], "level": "DEBUG" }
}
}
My question is how to use a JSON layout with this kind of configuration file ? I cannot find any documentation on that
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.