<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Where do I find the python log of a custom endpoint in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Where-do-I-find-the-python-log-of-a-custom-endpoint/m-p/318969#M4408</link>
    <description>&lt;P&gt;I did a spot check and it turned out that your first example is logging to var/log/splunk/python.log&lt;BR /&gt;
Anyway thanks for the hint with the logger! It's working!&lt;/P&gt;</description>
    <pubDate>Thu, 07 Sep 2017 06:23:06 GMT</pubDate>
    <dc:creator>manuel2202</dc:creator>
    <dc:date>2017-09-07T06:23:06Z</dc:date>
    <item>
      <title>Where do I find the python log of a custom endpoint</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Where-do-I-find-the-python-log-of-a-custom-endpoint/m-p/318967#M4406</link>
      <description>&lt;P&gt;I added a custom endpoint successfully. But where can I find the output of my print statement? &lt;BR /&gt;
Further more I like know where to find the startup log for my endpoint. For example in case of an error in my code.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunk.appserver.mrsparkle.controllers as controllers
from splunk.appserver.mrsparkle.lib.decorators import expose_page

class Controller(controllers.BaseController):
    @expose_page(must_login=False)
    def my_function(self, **kwargs) :

        print "MY LOG MESSAGE"
        return "HELLO WORLD"
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Sep 2017 10:53:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Where-do-I-find-the-python-log-of-a-custom-endpoint/m-p/318967#M4406</guid>
      <dc:creator>manuel2202</dc:creator>
      <dc:date>2017-09-06T10:53:33Z</dc:date>
    </item>
    <item>
      <title>Re: Where do I find the python log of a custom endpoint</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Where-do-I-find-the-python-log-of-a-custom-endpoint/m-p/318968#M4407</link>
      <description>&lt;P&gt;You need to create a log for it first.  Otherwise it will probably end up in splunkd.log aka index=_internal sourcetype=splunkd but only in the form of a stack trace when there's an exception... or maybe all output will end up there... not certain.  If you do create a logger for it though like below, you can control where it goes:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import splunk.mining.dcutils as dcu
logger = dcu.getLogger()

logger.info("This is info")
logger.error("this is error")
logger.warn("this is warn")
logger.exception("this is exception")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The above will certainly log to python.log.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import logging
from splunk.appserver.mrsparkle.lib.util import make_splunkhome_path

def setup_logger(name, level=logging.WARNING, maxBytes=25000000, backupCount=5):
    '''
    Set up a default logger.

    @param name: The log file name.
    @param level: The logging level.
    @param maxBytes: The maximum log file size before rollover.
    @param backupCount: The number of log files to retain.
    '''

    # Strip ".py" from the log file name if auto-generated by a script.
    if '.py' in name:
        name = name.replace(".py", "")

    logfile = make_splunkhome_path(["var", "log", "splunk", name + '.log'])

    logger = logging.getLogger(name)
    logger.propagate = False  # Prevent the log messages from being duplicated in the python.log file
    logger.level = level

    # Prevent re-adding handlers to the logger object, which can cause duplicate log lines
    handler_exists = any([True for h in logger.handlers if h.baseFilename == logfile])
    if not handler_exists:
        file_handler = logging.handlers.RotatingFileHandler(logfile, mode='a', maxBytes=maxBytes, backupCount=backupCount)
        formatter = logging.Formatter('%(asctime)s %(levelname)s pid=%(process)d tid=%(threadName)s file=%(filename)s:%(funcName)s:%(lineno)d | %(message)s')
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)

    return logger


logger = setup_logger('yourLogName', level=logging.DEBUG)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The above will create a logs in /opt/splunk/var/log/splunk/yourLogName.log&lt;/P&gt;</description>
      <pubDate>Wed, 06 Sep 2017 15:18:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Where-do-I-find-the-python-log-of-a-custom-endpoint/m-p/318968#M4407</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2017-09-06T15:18:03Z</dc:date>
    </item>
    <item>
      <title>Re: Where do I find the python log of a custom endpoint</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Where-do-I-find-the-python-log-of-a-custom-endpoint/m-p/318969#M4408</link>
      <description>&lt;P&gt;I did a spot check and it turned out that your first example is logging to var/log/splunk/python.log&lt;BR /&gt;
Anyway thanks for the hint with the logger! It's working!&lt;/P&gt;</description>
      <pubDate>Thu, 07 Sep 2017 06:23:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Where-do-I-find-the-python-log-of-a-custom-endpoint/m-p/318969#M4408</guid>
      <dc:creator>manuel2202</dc:creator>
      <dc:date>2017-09-07T06:23:06Z</dc:date>
    </item>
    <item>
      <title>Re: Where do I find the python log of a custom endpoint</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Where-do-I-find-the-python-log-of-a-custom-endpoint/m-p/318970#M4409</link>
      <description>&lt;P&gt;Good catch, I updated my answer.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Sep 2017 11:21:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Where-do-I-find-the-python-log-of-a-custom-endpoint/m-p/318970#M4409</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2017-09-07T11:21:04Z</dc:date>
    </item>
  </channel>
</rss>

