[6a3a178] | 1 | /* eslint no-underscore-dangle:0 */
|
---|
| 2 | const debug = require("debug")("log4js:logger");
|
---|
| 3 | const LoggingEvent = require("./LoggingEvent");
|
---|
| 4 | const levels = require("./levels");
|
---|
| 5 | const clustering = require("./clustering");
|
---|
| 6 | const categories = require("./categories");
|
---|
| 7 | const configuration = require("./configuration");
|
---|
| 8 |
|
---|
| 9 | const stackReg = /at (?:(.+)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
|
---|
| 10 | function defaultParseCallStack(data, skipIdx = 4) {
|
---|
| 11 | const stacklines = data.stack.split("\n").slice(skipIdx);
|
---|
| 12 | const lineMatch = stackReg.exec(stacklines[0]);
|
---|
| 13 | if (lineMatch && lineMatch.length === 6) {
|
---|
| 14 | return {
|
---|
| 15 | functionName: lineMatch[1],
|
---|
| 16 | fileName: lineMatch[2],
|
---|
| 17 | lineNumber: parseInt(lineMatch[3], 10),
|
---|
| 18 | columnNumber: parseInt(lineMatch[4], 10),
|
---|
| 19 | callStack: stacklines.join("\n")
|
---|
| 20 | };
|
---|
| 21 | }
|
---|
| 22 | return null;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * Logger to log messages.
|
---|
| 27 | * use {@see log4js#getLogger(String)} to get an instance.
|
---|
| 28 | *
|
---|
| 29 | * @name Logger
|
---|
| 30 | * @namespace Log4js
|
---|
| 31 | * @param name name of category to log to
|
---|
| 32 | * @param level - the loglevel for the category
|
---|
| 33 | * @param dispatch - the function which will receive the logevents
|
---|
| 34 | *
|
---|
| 35 | * @author Stephan Strittmatter
|
---|
| 36 | */
|
---|
| 37 | class Logger {
|
---|
| 38 | constructor(name) {
|
---|
| 39 | if (!name) {
|
---|
| 40 | throw new Error("No category provided.");
|
---|
| 41 | }
|
---|
| 42 | this.category = name;
|
---|
| 43 | this.context = {};
|
---|
| 44 | this.parseCallStack = defaultParseCallStack;
|
---|
| 45 | debug(`Logger created (${this.category}, ${this.level})`);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | get level() {
|
---|
| 49 | return levels.getLevel(
|
---|
| 50 | categories.getLevelForCategory(this.category),
|
---|
| 51 | levels.TRACE
|
---|
| 52 | );
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | set level(level) {
|
---|
| 56 | categories.setLevelForCategory(
|
---|
| 57 | this.category,
|
---|
| 58 | levels.getLevel(level, this.level)
|
---|
| 59 | );
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | get useCallStack() {
|
---|
| 63 | return categories.getEnableCallStackForCategory(this.category);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | set useCallStack(bool) {
|
---|
| 67 | categories.setEnableCallStackForCategory(this.category, bool === true);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | log(level, ...args) {
|
---|
| 71 | const logLevel = levels.getLevel(level, levels.INFO);
|
---|
| 72 | if (this.isLevelEnabled(logLevel)) {
|
---|
| 73 | this._log(logLevel, args);
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | isLevelEnabled(otherLevel) {
|
---|
| 78 | return this.level.isLessThanOrEqualTo(otherLevel);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | _log(level, data) {
|
---|
| 82 | debug(`sending log data (${level}) to appenders`);
|
---|
| 83 | const loggingEvent = new LoggingEvent(
|
---|
| 84 | this.category,
|
---|
| 85 | level,
|
---|
| 86 | data,
|
---|
| 87 | this.context,
|
---|
| 88 | this.useCallStack && this.parseCallStack(new Error())
|
---|
| 89 | );
|
---|
| 90 | clustering.send(loggingEvent);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | addContext(key, value) {
|
---|
| 94 | this.context[key] = value;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | removeContext(key) {
|
---|
| 98 | delete this.context[key];
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | clearContext() {
|
---|
| 102 | this.context = {};
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | setParseCallStackFunction(parseFunction) {
|
---|
| 106 | this.parseCallStack = parseFunction;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | function addLevelMethods(target) {
|
---|
| 111 | const level = levels.getLevel(target);
|
---|
| 112 |
|
---|
| 113 | const levelStrLower = level.toString().toLowerCase();
|
---|
| 114 | const levelMethod = levelStrLower.replace(/_([a-z])/g, g =>
|
---|
| 115 | g[1].toUpperCase()
|
---|
| 116 | );
|
---|
| 117 | const isLevelMethod = levelMethod[0].toUpperCase() + levelMethod.slice(1);
|
---|
| 118 |
|
---|
| 119 | Logger.prototype[`is${isLevelMethod}Enabled`] = function() {
|
---|
| 120 | return this.isLevelEnabled(level);
|
---|
| 121 | };
|
---|
| 122 |
|
---|
| 123 | Logger.prototype[levelMethod] = function(...args) {
|
---|
| 124 | this.log(level, ...args);
|
---|
| 125 | };
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | levels.levels.forEach(addLevelMethods);
|
---|
| 129 |
|
---|
| 130 | configuration.addListener(() => {
|
---|
| 131 | levels.levels.forEach(addLevelMethods);
|
---|
| 132 | });
|
---|
| 133 |
|
---|
| 134 | module.exports = Logger;
|
---|