hi,
I am evaluating splunk-sdk for node. My application throws up a lot of messages, some info, some warn, some error. I have a requirement to log specific error levels only and the level is determined via a cmd line argument
e.g with Loggly I would do something like this. With the code below loggly only logs errors and ignores other types of messages.
node app.js error
// create loggly transport
var error_level = process.argv[2];
var logglyTransport = new (winston.transports.Loggly)({
subdomain: xxxx,
inputToken: xxxxx,
auth: xxxx,
json: true,
level: error_level
});
How do we achieve the same with splunk-sdk?
Hi @grishbr
We support different log levels in the SDK. To set the level you use the LOG_LEVEL environment variable.
You can set the levels to one of the following:
var levels = {
"ALL": 4,
"INFO": 3,
"WARN": 2,
"ERROR": 1,
"NONE": 0
};
so for example you could set process.env.LOG_LEVEL = 3
in your JS code to choose just INFO, or you could just set the environment variable before you app executes.
In case you are interested, the code where this is implemented is here: https://github.com/splunk/splunk-sdk-javascript/blob/master/lib/log.js#L34
Let us know if this works for you and thanks for using our SDK!
Hi @grishbr
We support different log levels in the SDK. To set the level you use the LOG_LEVEL environment variable.
You can set the levels to one of the following:
var levels = {
"ALL": 4,
"INFO": 3,
"WARN": 2,
"ERROR": 1,
"NONE": 0
};
so for example you could set process.env.LOG_LEVEL = 3
in your JS code to choose just INFO, or you could just set the environment variable before you app executes.
In case you are interested, the code where this is implemented is here: https://github.com/splunk/splunk-sdk-javascript/blob/master/lib/log.js#L34
Let us know if this works for you and thanks for using our SDK!
@grishbr great, glad to help!
Thanks a lot! I now understand this whole thing pretty well. My code is now running fine 🙂
(cont)
The log level in the SDK does not correspond to events / data you send to Splunk. The level tells us how much information we should return to you as a user of the SDK. This data could either relate to information we generate within the SDK code, OR could correspond to results from the server. For example if the server sends back a response that contains messages, then we will filter those levels based on the log level setting.
Does that clarify?
As a side note you can easily use the LOG_LEVEL env variable in your own code so that only the selected level or above is outputted.
Hi @grishbr, I've looked at the code now. I think you are expecting the log levels to work differently than they are designed for. In the example you are setting the log level and then calling methods on the logger which are then sending log messages to Splunk using different log levels. It sounds like you are expecting to see that you would only see console output for the "error" events you sent. Is that correct?
I am quite sure I am missing something trivial. Can you pls take a look at my gist?
Have two js files: app.js requires splunkSdkLogger.js
I set process.env.LOG_LEVEL = 1 hoping to log only "ERROR" but that doesn't seem to work. It just logs everything.
I tried setting it with splunkjs.Logger.setLevel(1) -> no luck either
So that example is using a Winston logging transport (winston-loggly)
One of our community developers wrote a Splunk transport for Winston (winston-splunk).
There are many other Winston logging transports available that you could also use for Splunk.
I started with this. But I couldnt seem to get the log level working. So I moved on to the officially endorsed splunk-sdk