Skip to content

Instantly share code, notes, and snippets.

@mayurah
Last active October 10, 2022 04:22
Show Gist options
  • Save mayurah/6ad34ca4fbe9375c94c6c2f51ffe4cfe to your computer and use it in GitHub Desktop.
Save mayurah/6ad34ca4fbe9375c94c6c2f51ffe4cfe to your computer and use it in GitHub Desktop.
Python Snippet to log in File or Splunk HEC
import logging
import logging.handlers
import datetime
from os.path import expanduser
from splunk_hec_handler import SplunkHecHandler
def setup_logger(name, host=None, port=8088, token=None, verify=False):
logger = logging.getLogger(name)
logger.propagate = False
logger.setLevel(logging.DEBUG)
file_name = '{}.log'.format(name)
file_handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=25000000, backupCount=5)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
if host is not None and token is not None and HEC_LOGGING is not False:
splunk_handler = SplunkHecHandler(host, token, port=port, proto='https', ssl_verify=verify, source=name)
logger.addHandler(splunk_handler)
return logger
# With HEC
# logging = setup_logger("mylogfile", host=HEC_HOST, token=HEC_TOKEN)
# Without HEC
logging = setup_logger("mylogfile")
# ——————————————
import logging
import logging.handlers
from splunk_handler import SplunkHandler
import time
import datetime
from os.path import expanduser
import os
from constants import APP_NAME, SPLUNK_HOST, SPLUNK_PORT, SPLUNK_TOKEN, SPLUNK_INDEX
def setup_logger(name, host=None, port=8088, token=None, verify=False):
logger = logging.getLogger(name)
logger.propagate = False
logger.setLevel(logging.DEBUG)
file_name = '{}.log'.format(name)
file_handler = logging.handlers.RotatingFileHandler(file_name, maxBytes=25000000, backupCount=5)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
if host is not None and token is not None:
# splunk_handler = SplunkHecHandler(host, token, port=port, proto='https', ssl_verify=verify, source=name)
# logger.addHandler(splunk_handler)
splunk_handler = SplunkHandler(
host = os.environ.get("SPLUNK_HOST", SPLUNK_HOST) ,
port = os.environ.get("SPLUNK_PORT", SPLUNK_PORT),
token = os.environ.get("SPLUNK_TOKEN", SPLUNK_TOKEN),
index = os.environ.get("SPLUNK_INDEX", SPLUNK_INDEX),
verify=False, # turn SSL verification on or off, defaults to True
protocol='https',
debug=True
# flush_interval=0
#allow_overrides=True # whether to look for _<param in log data (ex: _index)
#debug=True # whether to print module activity to stdout, defaults to False
#flush_interval=15.0, # send batch of logs every n sec, defaults to 15.0, set '0' to block thread & send immediately
#force_keep_ahead=True # sleep instead of dropping logs when queue fills
#hostname='hostname', # manually set a hostname parameter, defaults to socket.gethostname()
#protocol='http', # set the protocol which will be used to connect to the splunk host
#proxies={
# 'http': 'http://10.10.1.10:3128',
# 'https': 'http://10.10.1.10:1080',
# }, set the proxies for the session request to splunk host
#
#queue_size=5000, # a throttle to prevent resource overconsumption, defaults to 5000, set to 0 for no max
#record_format=True, whether the log format will be json
#retry_backoff=1, the requests lib backoff factor, default options will retry for 1 min, defaults to 2.0
#retry_count=5, number of retry attempts on a failed/erroring connection, defaults to 5
#source='source', # manually set a source, defaults to the log record.pathname
#sourcetype='sourcetype', # manually set a sourcetype, defaults to 'text'
#timeout=60, # timeout for waiting on a 200 OK from Splunk server, defaults to 60s
)
logger.addHandler(splunk_handler)
return logger
# With HEC
logging = setup_logger(APP_NAME, host=SPLUNK_HOST, token=SPLUNK_TOKEN)
logging.info("info test")
logging.debug("debug test")
logging.error("err test")
# Without HEC
# logging = setup_logger("mylogfile")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment