How To Set Up A Splunk Logger

#1

In version 7.9 or later, C3.logger() will get the default logger for the current Type.

In your MyTypeName.js method, you can add statements like this to log items:

if (C3.logger().isDebugEnabled()) {
   C3.logger().debug("note about some variable you want to log: " + someVariableInYourJavascriptMethod);
}

Note that the someVariableInYourJavascriptMethod must be a string. JSON.stringify() can be helpful to turn an object into a string if you wish to log an object.

Now, the above statement will be logged to splunk IF you have turned on the logging to the correct level.

To turn on detailed logging, run this in console:

var loggerName = 'MyTypeName';
var loggerLevel = 'DEBUG'
var logLevelDuration = 600  // how long in seconds you wish the log to be turned on to the loggerLevel of detail.
Cluster.setLogLevel(loggerName, loggerLevel, logLevelDuration)

Then you can execute the method you implemented logging in and check splunk for details.

In version 7.8 or earlier, at the top of your TypeName.js file where you
are implementing the method you would like to log from, put the following
line at the top to instantiate your logger (following the following format
for easy splunk searching later):

var logger = C3.logger("packageNameTypeName"); // no periods or non-letter characters seem to be allowed here.

Then, in your .js method, you can add statements like this to log items:

    if (logger.isDebugEnabled()) {
       logger.debug("note about some variable you want to log: " + someVariableInYourJavascriptMethod);
    }

Note that the someVariableInYourJavascriptMethod must be a string.
JSON.stringify() can be helpful to turn an object into a string if you
wish to log an object.

Now, the above statement will be logged to splunk IF you have turned
on the logging to the correct level.

To turn on detailed logging, run this in console:

    var loggerName = 'packageNameTypeName';
    var loggerLevel = 'DEBUG'
    var logLevelDuration = 600  // how long in seconds you wish the log to be turned on to the loggerLevel of detail.
    Cluster.setLogLevel(loggerName, loggerLevel, logLevelDuration)

Then you can execute the method you implemented logging in and check splunk for details.

6 Likes
#2

Thank you @ColumbusL !
Is anyone know how to do the same properly in Python ? Or any resource that could be useful to know how to log in Splunk from Python ?

#3

Logging from python server code is possible from v7.8.6.
The usage is standard in python: use the logging module and the functions ”error”, ”warn”, ”info”, …

2 Likes
#4

As a note the if block is not necessary in the given example. If the servers log level is not set to “DEBUG” for that particular log name then the code will not actually log that message to splunk.

if (log.isDebugEnabled()) {
   log.debug("Your message");
}

Will act the same as

log.debug("Your message");

Given that there are still reasons why you would want to encompass your logging statement with an if block. If within your log function call you also do some processing then you will want to wrap it in an if block. For example:

if (log.isDebugEnabled()) {
   log.debug("Your message: " + JSON.stringify(myObj));
}

If you didn’t wrap the log.debug then every time the function is called myObj would be stringified. This would have a reduction in performance.

2 Likes
#5

[Update about Python]

To log into Splunk from Python code, simply create a logger:

# the name is up to you, it's just useful to search in Splunk
logger = logging.getLogger('MyPythonFile.py')
logger.setLevel(logging.INFO)

Then you can use it in your code, as a “standard” python logger:

logger.info('Starting job: %s', job.id)
1 Like