def getLogger( logging_file_name="%s.log" % app_name, logging_stanza_name="python", log_format=DEFAULT_LOGGING_FORMAT): """ source : http://dev.splunk.com/view/logging/SP-CAAAFCN """ logger = logging.getLogger(app_name) SPLUNK_HOME = os.environ.get('SPLUNK_HOME', '~') LOGGING_DEFAULT_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join( SPLUNK_HOME, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = logging_stanza_name LOGGING_FILE_NAME = logging_file_name BASE_LOG_PATH = os.path.join(SPLUNK_HOME, 'var', 'log', 'splunk') COMPLETE_PATH = os.path.join(BASE_LOG_PATH, LOGGING_FILE_NAME) if not os.path.exists(BASE_LOG_PATH): os.makedirs(BASE_LOG_PATH) LOGGING_FORMAT = log_format file_handler = logging.FileHandler(COMPLETE_PATH) splunk_log_handler = logging.handlers.RotatingFileHandler(COMPLETE_PATH, mode='a', maxBytes=MAX_FILE_SIZE, backupCount=5) splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(file_handler) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) logger.setLevel(logging.INFO) return logger
def setup_logging(self): logger = logging.getLogger('splunk.foo') SPLUNK_HOME = os.environ['SPLUNK_HOME'] LOGGING_DEFAULT_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = 'python' LOGGING_FILE_NAME = "nlp-text-analytics.log" BASE_LOG_PATH = os.path.join('var', 'log', 'splunk') LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler( os.path.join( SPLUNK_HOME, BASE_LOG_PATH, LOGGING_FILE_NAME ), mode='a') splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(splunk_log_handler) setupSplunkLogger( logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME ) return logger
def setup_logging(): """ Setup logging Log is written to /opt/splunk/var/log/splunk/octopus.log """ logger = logging.getLogger('splunk.octopus') logger.setLevel(logging.INFO) SPLUNK_HOME = os.environ['SPLUNK_HOME'] LOGGING_DEFAULT_CONFIG_FILE = os.path.join(_SPLUNK_HOME, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join(_SPLUNK_HOME, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = 'python' LOGGING_FILE_NAME = "octopus.log" BASE_LOG_PATH = os.path.join('var', 'log', 'splunk') LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler(os.path.join( _SPLUNK_HOME, BASE_LOG_PATH, LOGGING_FILE_NAME), mode='a') splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) return logger
def setup_logging(self): """ Setup logging Log is written to /opt/splunk/var/log/splunk/traffic_inv_kvstore.log """ FILENAME = os.path.splitext(os.path.basename(__file__))[0] logger = logging.getLogger('splunk.' + FILENAME) logger.setLevel(logging.INFO) SPLUNK_HOME = os.environ['SPLUNK_HOME'] LOGGING_DEFAULT_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = 'python' LOGGING_FILE_NAME = FILENAME + ".log" BASE_LOG_PATH = os.path.join('var', 'log', 'splunk') LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler(os.path.join( SPLUNK_HOME, BASE_LOG_PATH, LOGGING_FILE_NAME), mode='a') splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) # some logging in case of successful initialization. logger.info("Script started and logging initialized correctly.") return logger
def setup_logging(self): self.logger = logging.getLogger('splunk.SplunkAppforAWSBilling') LOGGING_DEFAULT_CONFIG_FILE = os.path.join(self.splunk_home, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join(self.splunk_home, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = 'python' LOGGING_FILE_NAME = "SplunkAppforAWSBilling.log" BASE_LOG_PATH = os.path.join('var', 'log', 'splunk') LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler(os.path.join(self.splunk_home, BASE_LOG_PATH, LOGGING_FILE_NAME), mode='a') splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) self.logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(self.logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME)
def setup_logging(): logger = logging.getLogger('splunk.script') SPLUNK_HOME = os.environ['SPLUNK_HOME'] # define logging configuration. taken from python-site/splunk/appserver/mrsparkle/root.py LOGGING_DEFAULT_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = 'python' BASE_LOG_PATH = os.path.join('var', 'log', 'splunk') LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler(os.path.join(SPLUNK_HOME, BASE_LOG_PATH, 'search_script.log'), mode='a') splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) return logger
def setup_logger(): logger = logging.getLogger('splunk.octoblu') SPLUNK_HOME = os.environ['SPLUNK_HOME'] LOGGING_DEFAULT_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = 'python' LOGGING_FILE_NAME = "octoblu.log" BASE_LOG_PATH = os.path.join('var', 'log', 'splunk') LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler(os.path.join(SPLUNK_HOME, BASE_LOG_PATH, LOGGING_FILE_NAME), mode='a') splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) return logger
def setup_logging(): logger = logging.getLogger("splunk.script") SPLUNK_HOME = os.environ["SPLUNK_HOME"] # define logging configuration. taken from python-site/splunk/appserver/mrsparkle/root.py LOGGING_DEFAULT_CONFIG_FILE = os.path.join(SPLUNK_HOME, "etc", "log.cfg") LOGGING_LOCAL_CONFIG_FILE = os.path.join(SPLUNK_HOME, "etc", "log-local.cfg") LOGGING_STANZA_NAME = "python" BASE_LOG_PATH = os.path.join("var", "log", "splunk") LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler( os.path.join(SPLUNK_HOME, BASE_LOG_PATH, "search_script.log"), mode="a" ) splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) return logger
def setup_logging(): #Copied from http://dev.splunk.com/view/splunk-extensions/SP-CAAAEA9 logger = logging.getLogger('splunk.monitis-lookup') SPLUNK_HOME = os.environ['SPLUNK_HOME'] LOGGING_DEFAULT_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = 'python' LOGGING_FILE_NAME = "monitis-lookup.log" BASE_LOG_PATH = os.path.join('var', 'log', 'splunk') LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler(os.path.join(SPLUNK_HOME, BASE_LOG_PATH, LOGGING_FILE_NAME), mode='a') splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) return logger
def setup_logging(n): logger = logging.getLogger(n) if myDebug == 'yes': logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.ERROR) LOGGING_DEFAULT_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = 'python' LOGGING_FILE_NAME = '%s.log' % myScript BASE_LOG_PATH = os.path.join('var', 'log', 'splunk') LOGGING_FORMAT = '%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s' splunk_log_handler = logging.handlers.RotatingFileHandler(os.path.join(SPLUNK_HOME, BASE_LOG_PATH, LOGGING_FILE_NAME), mode='a') splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) return logger
def _setup_logging_splunk_configs(logger): ''' Read logging level information from config files to overwrite log level :param logger - the logger :return the logger with logging level overwritten ''' logging_default_config_file = make_splunkhome_path(['etc', 'log.cfg']) logging_local_config_file = make_splunkhome_path(['etc', 'log-local.cfg']) logging_stanza_name = 'python' setupSplunkLogger( logger, logging_default_config_file, logging_local_config_file, logging_stanza_name, verbose=False) return logger
def setup_logging(): #Copied from http://dev.splunk.com/view/splunk-extensions/SP-CAAAEA9 logger = logging.getLogger('splunk.monitis-lookup') SPLUNK_HOME = os.environ['SPLUNK_HOME'] LOGGING_DEFAULT_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = 'python' LOGGING_FILE_NAME = "monitis-lookup.log" BASE_LOG_PATH = os.path.join('var', 'log', 'splunk') LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler(os.path.join( SPLUNK_HOME, BASE_LOG_PATH, LOGGING_FILE_NAME), mode='a') splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) return logger
def setup_logging(self): """ Just set up some splunky logging goodness. :return: """ self.logger = logging.getLogger("splunk.SplunkAppforAWSBilling") LOGGING_DEFAULT_CONFIG_FILE = os.path.join(self.splunk_home, "etc", "log.cfg") LOGGING_LOCAL_CONFIG_FILE = os.path.join(self.splunk_home, "etc", "log-local.cfg") LOGGING_STANZA_NAME = "python" LOGGING_FILE_NAME = "SplunkAppforAWSBilling.log" BASE_LOG_PATH = os.path.join("var", "log", "splunk") LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t%(module)s:%(lineno)d - %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler( os.path.join(self.splunk_home, BASE_LOG_PATH, LOGGING_FILE_NAME), mode="a" ) splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) self.logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger( self.logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME )
def setup_logging(): global logger if not logger: logger = logging.getLogger("splunk.r") if "SPLUNK_HOME" in os.environ: splunk_home = os.environ["SPLUNK_HOME"] logging_default_config_file = os.path.join(splunk_home, "etc", "log.cfg") logging_local_config_file = os.path.join(splunk_home, "etc", "log-local.cfg") logging_stanza_name = "python" logging_file_name = "r.log" base_log_path = os.path.join("var", "log", "splunk") logging_format = "%(asctime)s %(levelname)s %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler( os.path.join(splunk_home, base_log_path, logging_file_name), mode="a" ) splunk_log_handler.setFormatter(logging.Formatter(logging_format)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger( logger, logging_default_config_file, logging_local_config_file, logging_stanza_name ) return logger
def setup_logging(): """ setup_logging as found on http://dev.splunk.com/view/logging/SP-CAAAFCN """ logger = logging.getLogger('splunk.shareprivateobjects') SPLUNK_HOME = os.environ['SPLUNK_HOME'] LOGGING_DEFAULT_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log.cfg') LOGGING_LOCAL_CONFIG_FILE = os.path.join(SPLUNK_HOME, 'etc', 'log-local.cfg') LOGGING_STANZA_NAME = 'python' LOGGING_FILE_NAME = "changedispatchttlall.log" BASE_LOG_PATH = os.path.join('var', 'log', 'splunk') LOGGING_FORMAT = "%(asctime)s %(levelname)-s\t %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler(os.path.join( SPLUNK_HOME, BASE_LOG_PATH, LOGGING_FILE_NAME), mode='a') splunk_log_handler.setFormatter(logging.Formatter(LOGGING_FORMAT)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) return logger
def get_logger(self, app_name=None, file_name="kenny_loggins", log_level=logging.INFO): log_location = make_splunkhome_path(['var', 'log', 'splunk', app_name]) # logging.setLoggerClass(SplunkLogger) LOGGING_DEFAULT_CONFIG_FILE = make_splunkhome_path( ['etc', 'apps', app_name, 'default', 'log.cfg']) LOGGING_LOCAL_CONFIG_FILE = make_splunkhome_path( ['etc', 'apps', app_name, 'local', 'log.cfg']) LOGGING_STANZA_NAME = app_name _log = logging.getLogger("{}".format(file_name)) if not os.path.isdir(log_location): os.mkdir(log_location) output_file_name = os.path.join(log_location, "{}.log".format(file_name)) _log.propogate = False _log.setLevel(logging.INFO) f_handle = handlers.RotatingFileHandler(output_file_name, maxBytes=25000000, backupCount=5) formatter = logging.Formatter( '%(asctime)s log_level=%(levelname)s pid=%(process)d tid=%(threadName)s file="%(filename)s" function="%(funcName)s" line_number="%(lineno)d" version="{}" %(message)s' .format(gmail_version.__version__)) f_handle.setFormatter(formatter) if not len(_log.handlers): _log.addHandler(f_handle) try: splunk.setupSplunkLogger(_log, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) except Exception as e: _log.setLevel(logging.DEBUG) _log.error( "Failed to setup Logger {3}:{4}: {1}:{0}, setting log_level to {2}" .format(e, type(e), log_level, app_name, file_name)) _log.setLevel(log_level) return _log
def setup_logging(): global logger if not logger: logger = logging.getLogger('splunk.r') if 'SPLUNK_HOME' in os.environ: splunk_home = os.environ['SPLUNK_HOME'] logging_default_config_file = os.path.join(splunk_home, 'etc', 'log.cfg') logging_local_config_file = os.path.join(splunk_home, 'etc', 'log-local.cfg') logging_stanza_name = 'python' logging_file_name = "r.log" base_log_path = os.path.join('var', 'log', 'splunk') logging_format = "%(asctime)s %(levelname)s %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler( os.path.join(splunk_home, base_log_path, logging_file_name), mode='a') splunk_log_handler.setFormatter(logging.Formatter(logging_format)) logger.addHandler(splunk_log_handler) splunk.setupSplunkLogger(logger, logging_default_config_file, logging_local_config_file, logging_stanza_name) return logger
def setup_logging(logging_level: int): logger = logging.getLogger('event_masker_command.log') logger.setLevel(logging_level) splunk_home = os.environ['SPLUNK_HOME'] logging_file_name = "event_masker.log" base_log_path = os.path.join('var', 'log', 'splunk') logging_format = "%(asctime)s %(levelname)s %(message)s" splunk_log_handler = logging.handlers.RotatingFileHandler( os.path.join(splunk_home, base_log_path, logging_file_name), mode='a', maxBytes=25000000, backupCount=5) splunk_log_handler.setFormatter(logging.Formatter(logging_format)) logger.addHandler(splunk_log_handler) logging_default_config_file = os.path.join(splunk_home, 'etc', 'log.cfg') logging_local_config_file = os.path.join(splunk_home, 'etc', 'log-local.cfg') logging_stanza_name = 'python' splunk.setupSplunkLogger(logger, logging_default_config_file, logging_local_config_file, logging_stanza_name) return logger
class SafeXMLParser(lxml.etree.XMLParser): """An XML Parser that ignores requests for external entities""" def __init__(self, *a, **kw): super(SafeXMLParser, self).__init__(*a, **kw) self.resolvers.add(NullResolver()) parser = SafeXMLParser() lxml.etree.set_default_parser(parser) lxml.etree.UnsafeXMLParser = lxml.etree.XMLParser lxml.etree.XMLParser = SafeXMLParser try: splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) # # continue importing # import re, time, shutil, hashlib import splunk.clilib.cli_common import splunk.util, splunk.entity from controllers.top import * import cherrypy from lib.util import make_absolute, splunk_to_cherry_cfg, get_rss_parent_dir, make_splunkhome_path, is_encrypted_cert from lib import i18n, filechain, error, startup from lib.customlogmanager import SplunkedLogManager from splunk.appserver.mrsparkle.lib import module
def setup_logging(logfile_name=None, logger_name=None, logger=None, level=logging.INFO, is_console_header=False, log_format=LOG_DEFAULT_FMT, is_propagate=False): ''' Setup logging @param logfile_name: log file name @param logger_name: logger name (if logger specified then we ignore this argument) @param logger: logger object @param level: logging level @param is_console_header: set to true if console logging is required @param log_format: log message format @param is_propagate: set to true if you want to propagate log to higher level @return: logger ''' if (logfile_name is None or logger_name is None) and logger is None: raise ValueError(( "log_name or logger_name is not specified and logger object is not provided." )) if logger is None: # Logger is singleton so if logger is already defined it will return old handler logger = logging.getLogger(logger_name) logger = logger.logger if isinstance(logger, CloudgatewayLogger) else logger # Save the handlers before overwriting logger loghandlers = logger.handlers # If handlers is already defined then do not create new handler, this way we can avoid file opening again # which is issue on windows see ITOA-2439 for more information # Now we are checking if we need create new handler(s) hasFileHandler = False hasConsoleHandler = False handlerFormat = None for handler in loghandlers: if isinstance(handler, logging.handlers.RotatingFileHandler): handlerFormat = handlerFormat if handlerFormat else handler.formatter hasFileHandler = True elif isinstance(handler, logging.StreamHandler): handlerFormat = handlerFormat if handlerFormat else handler.formatter hasConsoleHandler = True # If logger_name is None: will create a child logger with different properties from parent. # If the given logger_name is not equal to the existing logger's name, also will create a child logger if logger_name is None or logger.name != logger_name: # dot(.) in the two log names make the new logger is the child of the existing logger, # so new handlers added to the new one will not impact the old one. logger = logging.getLogger( "%s.%s" % (logger.name, logger_name if logger_name else "sub")) logger.propagate = is_propagate # Prevent the log messages from being duplicated in the python.log file logger.setLevel(level) if not hasFileHandler: try: lockdir = make_splunkhome_path(['var', CLOUDGATEWAY, 'lock']) if not os.path.exists(os.path.dirname(lockdir)): os.mkdir(make_splunkhome_path(['var', CLOUDGATEWAY])) os.mkdir(make_splunkhome_path(['var', CLOUDGATEWAY, 'lock'])) elif not os.path.exists(lockdir): os.mkdir(lockdir) except OSError as ose: #Swallow all "File exists" errors - another thread/process beat us to the punch if ose.errno != 17: raise logfile = logfile_name if os.path.basename(logfile_name) == logfile_name: logfile = make_splunkhome_path( ['var', 'log', 'splunk', logfile_name]) #Note that there are still some issues with windows here, going to make it so that we dont file_handler = logging.handlers.RotatingFileHandler(logfile, maxBytes=2500000, backupCount=5) file_handler.setFormatter( handlerFormat if handlerFormat else logging.Formatter(log_format)) logger.addHandler(file_handler) if is_console_header and not hasConsoleHandler: console_handler = logging.StreamHandler() console_handler.setFormatter( handlerFormat if handlerFormat else logging.Formatter(log_format)) logger.addHandler(console_handler) # Read logging level information from log.cfg so it will overwrite log # Note if logger level is specified on that file then it will overwrite log level LOGGING_DEFAULT_CONFIG_FILE = make_splunkhome_path(['etc', 'log.cfg']) LOGGING_LOCAL_CONFIG_FILE = make_splunkhome_path(['etc', 'log-local.cfg']) LOGGING_STANZA_NAME = 'python' setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME, verbose=False) return logger
#Swallow all "File exists" errors - another thread/process beat us to the punch if ose.errno != 17: raise #Note that there are still some issues with windows here, going to make it so that we dont file_handler = logging.handlers.RotatingFileHandler(make_splunkhome_path(['var', 'log', 'splunk', log_name]), maxBytes=2500000, backupCount=5) formatter = logging.Formatter(log_format) file_handler.setFormatter(formatter) logger.addHandler(file_handler) # Console stream handler if is_console_header: console_handler = logging.StreamHandler() console_handler.setFormatter(logging.Formatter(log_format)) logger.addHandler(console_handler) # Read logging level information from log.cfg so it will overwrite log # Note if logger level is specified on that file then it will overwrite log level LOGGING_DEFAULT_CONFIG_FILE = make_splunkhome_path(['etc', 'log.cfg']) LOGGING_LOCAL_CONFIG_FILE = make_splunkhome_path(['etc', 'log-local.cfg']) LOGGING_STANZA_NAME = 'python' setupSplunkLogger( logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME, verbose=False ) return logger
def get_logger(name=BASE_LOGGER_NAME, level=DEFAULT_LEVEL): """Returns a general-purpose logger instance. The logger is configured to write to both: * A (rotated) file in $SPLUNK_HOME/var/log/splunk/<name>.log * Standard error. Additionally, it consults $SPLUNK_HOME/etc/log.cfg and log-local.cfg for default log-levels. You can configure per-logger log-levels by adding a property to log-local.cfg that looks like: [python] myloggername = DEBUG For DEBUG messages to show up in search.log as well, you will need to modify $SPLUNK_HOME/etc/log-searchprocess-local.cfg to contain: category.ChunkedExternProcessor=DEBUG Idiomatic usage is: #!/usr/bin/env python import setup_logging logger = setup_logging.get_logger() def foo(): logger.warn("Red Alert, report to battle stations") """ logger = logging.getLogger(name) # Initial setup if len(logger.handlers) == 0: logger.setLevel(level) logger.propagate = False has_splunk_home = os.environ.get('SPLUNK_HOME') if has_splunk_home: path = make_splunkhome_path( ['var', 'log', 'splunk', name + '.log']) backup_count = 5 else: # No backups if logging to current directory path = os.path.normpath(os.path.join(os.getcwd(), name + '.log')) backup_count = 0 file_handler = logging.handlers.RotatingFileHandler( path, maxBytes=1000000, backupCount=backup_count) formatter = logging.Formatter( '%(created)f PID %(process)d %(asctime)s %(levelname)s [%(name)s] [%(funcName)s] %(message)s' ) file_handler.setFormatter(formatter) logger.addHandler(file_handler) stream_handler = logging.StreamHandler() stream_handler.setFormatter( logging.Formatter('%(levelname)s %(message)s')) logger.addHandler(stream_handler) if has_splunk_home: try: import splunk # Read logging level information from log.cfg so it will overwrite log # Note if logger level is specified on that file then it will overwrite log level LOGGING_DEFAULT_CONFIG_FILE = make_splunkhome_path( ['etc', 'log.cfg']) LOGGING_LOCAL_CONFIG_FILE = make_splunkhome_path( ['etc', 'log-local.cfg']) LOGGING_STANZA_NAME = 'python' splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME, verbose=False) except ImportError: warning_msg = ( 'Unable to import splunk python module: cannot setup splunk logging. ' 'If you are using the Splunk Machine Learning Toolkit\'s code directly, ' 'without a custom search command, you may see this warning.' ) logger.warn(warning_msg) else: logger.warn('No SPLUNK_HOME set. Logging to %s', path) return logger
def get_logger(name=BASE_LOGGER_NAME, level=DEFAULT_LEVEL): """Returns a general-purpose logger instance. The logger is configured to write to both: * A (rotated) file in $SPLUNK_HOME/var/log/splunk/<name>.log * Standard error. Additionally, it consults $SPLUNK_HOME/etc/log.cfg and log-local.cfg for default log-levels. You can configure per-logger log-levels by adding a property to log-local.cfg that looks like: [python] myloggername = DEBUG For DEBUG messages to show up in search.log as well, you will need to modify $SPLUNK_HOME/etc/log-searchprocess-local.cfg to contain: category.ChunkedExternProcessor=DEBUG Idiomatic usage is: #!/usr/bin/env python import setup_logging logger = setup_logging.get_logger() def foo(): logger.warn("Red Alert, report to battle stations") """ logger = logging.getLogger(name) # Initial setup if len(logger.handlers) == 0: logger.setLevel(level) logger.propagate = False path = make_splunkhome_path(['var', 'log', 'splunk', name + '.log']) file_handler = logging.handlers.RotatingFileHandler(path, maxBytes=1000000, backupCount=5) formatter = logging.Formatter( '%(created)f %(asctime)s %(levelname)s [%(name)s] [%(funcName)s] %(message)s' ) file_handler.setFormatter(formatter) logger.addHandler(file_handler) stream_handler = logging.StreamHandler() stream_handler.setFormatter( logging.Formatter('%(levelname)s %(message)s')) logger.addHandler(stream_handler) # Read logging level information from log.cfg so it will overwrite log # Note if logger level is specified on that file then it will overwrite log level LOGGING_DEFAULT_CONFIG_FILE = make_splunkhome_path(['etc', 'log.cfg']) LOGGING_LOCAL_CONFIG_FILE = make_splunkhome_path( ['etc', 'log-local.cfg']) LOGGING_STANZA_NAME = 'python' splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME, verbose=False) return logger
class SafeXMLParser(lxml.etree.XMLParser): """An XML Parser that ignores requests for external entities""" def __init__(self, *a, **kw): super(SafeXMLParser, self).__init__(*a, **kw) self.resolvers.add(NullResolver()) parser = SafeXMLParser() lxml.etree.set_default_parser(parser) lxml.etree.UnsafeXMLParser = lxml.etree.XMLParser lxml.etree.XMLParser = SafeXMLParser try: splunk.setupSplunkLogger(logger, LOGGING_DEFAULT_CONFIG_FILE, LOGGING_LOCAL_CONFIG_FILE, LOGGING_STANZA_NAME) # # continue importing # import re, time, shutil, hashlib import splunk.clilib.cli_common import splunk.clilib.bundle_paths import splunk.util, splunk.entity from controllers.top import * import cherrypy from lib.util import make_absolute, splunk_to_cherry_cfg, get_rss_parent_dir, make_splunkhome_path, is_encrypted_cert from lib import i18n, filechain, error, startup from lib.customlogmanager import SplunkedLogManager