-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db3ccc4
commit 00bd3db
Showing
6 changed files
with
349 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,113 @@ | ||
const pinoDebug = require("pino-debug"); | ||
const pino = require("pino"); | ||
const util = require("util"); | ||
|
||
const logger = pino({ | ||
formatters: { | ||
level: (label) => { | ||
return { | ||
level: label, | ||
}; | ||
}, | ||
}, | ||
level: "debug", | ||
}); | ||
|
||
const map = { | ||
"*": "debug", | ||
}; | ||
|
||
const config = { | ||
auto: false, | ||
map, | ||
}; | ||
|
||
pinoDebug(logger, config); | ||
|
||
const path = require("path"); | ||
const $debug = require("debug"); | ||
|
||
function byPrecision(a, b) { | ||
var aix = a.indexOf("*"); | ||
var bix = b.indexOf("*"); | ||
if (aix === -1 && bix === -1) return 0; | ||
if (~aix && ~bix) { | ||
if (aix > bix) return -1; | ||
if (aix < bix) return 1; | ||
return a.length < b.length ? 1 : a.length === b.length ? 0 : -1; | ||
} | ||
return ~bix ? -1 : 1; | ||
} | ||
|
||
const root = `${path.dirname( | ||
(require.main && require.main.filename) || process.filename, | ||
)}/`; | ||
|
||
function logger(caller) { | ||
let name = caller.replace(root, "").split("."); | ||
module.exports = function logger(caller) { | ||
let name = caller.replace(root, "").replace("/", ":").split("."); | ||
if (name.length > 1) { | ||
name.pop(); | ||
} | ||
name = name.join(); | ||
const debug = $debug(`debug:vao-backend/${name}`); | ||
const info = $debug(`info:vao-backend/${name}`); | ||
const warn = $debug(`warn:vao-backend/${name}`); | ||
|
||
const debugNS = `debug:vao-backend:${name}`; | ||
const infoNS = `info:vao-backend:${name}`; | ||
const warnNS = `warn:vao-backend:${name}`; | ||
|
||
if (!config.auto) { | ||
map[debugNS] = "debug"; | ||
map[infoNS] = "info"; | ||
map[warnNS] = "warn"; | ||
|
||
$debug.map = Object.keys(map) | ||
.sort(byPrecision) | ||
.reduce(function (m, k) { | ||
m.set( | ||
RegExp( | ||
"^" + | ||
k.replace(/[\\^$+?.()|[\]{}]/g, "\\$&").replace(/\*/g, ".*?") + | ||
"$", | ||
), | ||
map[k], | ||
); | ||
return m; | ||
}, new Map()); | ||
} | ||
|
||
const debug = $debug(debugNS); | ||
const info = $debug(infoNS); | ||
const warn = $debug(warnNS); | ||
|
||
console.info(debug); | ||
|
||
if (debug.enabled && !info.enabled) { | ||
info.enabled = true; | ||
} | ||
|
||
if (info.enabled && !warn.enabled) { | ||
warn.enabled = true; | ||
} | ||
|
||
return { | ||
d(message, ...args) { | ||
if (debug.enabled) { | ||
debug("[DEBUG]", message, ...args); | ||
} | ||
debug(util.format(message, ...args)); | ||
}, | ||
i(message, ...args) { | ||
console.info( | ||
"INFO", | ||
util.format(message, ...args), | ||
info, | ||
map[info.namespace], | ||
); | ||
if (debug.enabled) { | ||
debug("[INFO]", message, ...args); | ||
} else if (info.enabled) { | ||
info("[INFO]", message); | ||
info(util.format(message, ...args)); | ||
} else { | ||
info(message); | ||
} | ||
}, | ||
w(...args) { | ||
if (debug.enabled) { | ||
debug("[WARN]", ...args); | ||
} else if (info.enabled) { | ||
info("[WARN]", ...args); | ||
} else if (warn.enabled) { | ||
warn("[WARN]", ...args); | ||
} | ||
// Raven.captureException(e) | ||
console.info("WARN", util.format(...args), warn, map[warn.namespace]); | ||
warn(util.format(...args)); | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = logger; | ||
}; |
Oops, something went wrong.