logdna / logdna-winston

LogDNA's Node.js logging module with support for Winston
MIT License
16 stars 14 forks source link

Map Winston levels to LogDNA levels #31

Closed kendallroth closed 3 years ago

kendallroth commented 3 years ago

In our project there is a discrepancy between the log levels supported by @logdna/logger and the levels we are using in WinstonJS (we also use CRITICAL). We have not found a way to "map" between the Winston log levels (custom or otherwise) to the list of supported LogDNA levels.

For example, a potential workaround would be mapping our WinstonJS critical log level to the LogDNA error log level (since they do not support critical). However, currently this is not possible, so any of our critical logs get reported as info in LogDNA.

Is there any way to get support for additional log levels, or at least mapping them?

P.S. The LogDNA docs apparently support more common log levels, but their package does not... See slightly different ask in @logdna/logger

kendallroth commented 3 years ago

While debugging this I came across a few potential issues. Most notably, this library only supports the NPM log levels; however, WinstonJS provides several log level configurations (docs)! Furthermore, this library does not support custom log levels, as the mapping between WinstonJS and LogDNA levels is hardcoded (file)! This is quite frustrating for developers working with custom log levels, as it means there is no way to pass some log levels through to LogDNA (or map them).

Is there a way that this level mapping could be customizable?

kendallroth commented 3 years ago

As a workaround, I created a patch file with patch-package to resolve our specific issue. Ideally however, it would be best to provide a custom log level mapping instead of generating patch files. Other portion to get this working was in the @logdna/logger repository (another custom patch).

Please note these are intended as temporary workarounds only :)

diff --git a/node_modules/logdna-winston/index.js b/node_modules/logdna-winston/index.js
index e17031a..81abcf9 100644
--- a/node_modules/logdna-winston/index.js
+++ b/node_modules/logdna-winston/index.js
@@ -6,7 +6,8 @@ const pkg = require('./package.json')

 // Convert between Winston levels and @logdna/logger levels
 const levelTranslate = new Map([
-  ['error', 'error']
+  ['critical', 'critical']
+, ['error', 'error']
 , ['warn', 'warn']
 , ['info', 'info']
 , ['http', 'debug']
kendallroth commented 3 years ago

@darinspivey Regarding your brief mention of this repository in Issue #53, will there also be a change to support a custom Map (or something) between potentially custom WinstonJS log levels and the supported (and maybe custom) LogDNA levels?

darinspivey commented 3 years ago

@kendallroth Yes, once custom levels are supported, this package will get a major change to allow the usage of custom log levels. TBD.

darinspivey commented 3 years ago

@kendallroth Please let us know how this change works for you.

logdnabot commented 3 years ago

:tada: This issue has been resolved in version 4.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

kendallroth commented 3 years ago

@darinspivey Thank you so much for your work on this; I have tested (albeit not in production yet) and verified that this resolves our use case. Simply being able to use the custom Winston levels along with a quick call to Object.keys() is extremely nice!

P.S. While testing this I did run into an unrelated bug with the transport level option (used differently by LogDNA and WinstonJS), but have opened a separate issue for that)