Splunk Dev

After adding logging statements into Python script and running it via Splunk CLI, why are there no log messages in python.log?

ConnorG
Path Finder

I'm trying to debug a Python script and in doing so placed some logging statements into the script.

Following these posts for the basics:
Logging from Python in Splunk
Python logging in splunk

I'm going off the simple statement here
"You can also just call logging directly and your logs will appear in python.log:"

 logging.warning("Something bad happened: %s", "out of memory")

so my code looks just like

import logging
logging.info("message here")

Running it using the Splunk CLI command:

 splunk cmd python ./confcheck_correlation_searches.py

And yet still nothing in python.log. Any ideas what I'm missing here?

jkat54
SplunkTrust
SplunkTrust

The first link you showed has the answer wrapped up in a function. Here it is without being wrapped into a function:

# create logger object for logging - see https://docs.python.org/2/howto/logging.html for details
import logging
logger = logging.getLogger('confcheck_correlation_searches.py')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler('/var/log/splunk/confcheck_correlation_searches.log')
fh.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s | src="%(name)s" | lvl="%(levelname)s" | msg="%(message)s"')
fh.setFormatter(formatter)
logger.addHandler(fh)

Note, you may wish to change the formatter... i just grabbed the first example I had.

ConnorG
Path Finder

Yes I noticed this, but if you read further down it also states that

"You can also just call logging directly and your logs will appear in python.log"

As I followed in my example.

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

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

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...