Splunk Dev

Set logging levels with splunk sdk

girishbr
Explorer

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?

Tags (1)
0 Karma
1 Solution

gblock_splunk
Splunk Employee
Splunk Employee

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!

View solution in original post

gblock_splunk
Splunk Employee
Splunk Employee

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!

gblock_splunk
Splunk Employee
Splunk Employee

@grishbr great, glad to help!

0 Karma

girishbr
Explorer

Thanks a lot! I now understand this whole thing pretty well. My code is now running fine 🙂

0 Karma

gblock_splunk
Splunk Employee
Splunk Employee

(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.

gblock_splunk
Splunk Employee
Splunk Employee

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?

girishbr
Explorer

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

https://gist.github.com/techmechie/12d59a117d2db94d5901

0 Karma

girishbr
Explorer

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

0 Karma

Damien_Dallimor
Ultra Champion

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.

girishbr
Explorer

I started with this. But I couldnt seem to get the log level working. So I moved on to the officially endorsed splunk-sdk

0 Karma
Get Updates on the Splunk Community!

How to Monitor Google Kubernetes Engine (GKE)

We’ve looked at how to integrate Kubernetes environments with Splunk Observability Cloud, but what about ...

Index This | How can you make 45 using only 4?

October 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with this ...

Splunk Education Goes to Washington | Splunk GovSummit 2024

If you’re in the Washington, D.C. area, this is your opportunity to take your career and Splunk skills to the ...