def cli(config, debug): # Load site & configuration. os.environ['VLNA_SETTINGS'] = realpath(config) from vlna.site import site # Enable debugging if specified on the commmand line. site.config['DEBUG'] = site.config['DEBUG'] or debug # Get relevant configuration options. debug = site.config.get('DEBUG', False) host = site.config.get('HOST', '::') port = int(site.config.get('PORT', 5000)) # Set up the logging. level = DEBUG if debug else INFO handler = StreamHandler() handler.setLevel(level) handler.setFormatter(Formatter('%(levelname)s: [%(name)s] %(message)s')) root.addHandler(handler) root.setLevel(level) # Dump current configuration to the log. log.debug('Configuration:') for key, value in sorted(site.config.items()): log.debug(' %s = %r', key, value) # Prepare WSGI handler for the web site. handler = WSGIHandler(site) app = Application(debug=debug) app.router.add_route('*', '/{path_info:.*}', handler) # Run the web server / asyncio loop. run_app(app, host=host, port=port)
def getLogger(name): global handler if not os.path.exists(LOGDIR): os.mkdir(LOGDIR) if handler is None: path = os.path.join(LOGDIR, LOGFILE) handler = RotatingFileHandler(path, maxBytes=0x100000, backupCount=5) handler.setFormatter(Formatter(FMT)) root.setLevel(logging.INFO) root.addHandler(handler) log = logging.getLogger(name) return log
def configure_root_logger(silent, verbose): root_logger.addHandler(IGVMLogHandler()) # We are summing up the silent and verbose arguments in here. It # is not really meaningful to use them both, but giving an error is not # better. See Python logging library documentation [1] for the levels. # Paramiko is overly verbose. We configure it for one level higher. # # [1] https://docs.python.org/library/logging.html#logging-levels level = 20 + (silent - verbose) * 10 root_logger.setLevel(level) root_logger.getChild('paramiko').setLevel(level + 10)
def getLogger(name=NAME): global handler if not os.path.exists(LOGDIR): os.mkdir(LOGDIR) if handler is None: path = os.path.join(LOGDIR, LOGFILE) handler = RotatingFileHandler(path, maxBytes=0x100000, backupCount=5) handler.setFormatter(Formatter(FMT)) root.setLevel(logging.INFO) root.addHandler(handler) log = logging.getLogger(name) return log
def getLogger(name): global handler logdir = __logdir() if not os.path.exists(logdir): os.mkdir(logdir) if handler is None: path = logfile() handler = RotatingFileHandler(path, maxBytes=0x100000, backupCount=5) handler.setFormatter(Formatter(FMT)) root.setLevel(logging.INFO) root.addHandler(handler) log = logging.getLogger(name) return log
def log_collection_context(logging_memory_handler, tempdir, prefix, optional_archive_path=None): from logging import root, DEBUG path = get_tar_path(prefix, optional_archive_path) root.addHandler(logging_memory_handler) root.setLevel(DEBUG) try: yield path finally: with open_archive(path) as archive: logging_memory_handler.flush() logging_memory_handler.close() add_directory(archive, tempdir) print("Logs collected successfully to {!r}".format(path))
def getLogger(name): global handler logdir = __logdir() if not os.path.exists(logdir): os.mkdir(logdir) if handler is None: try: level = int(os.environ["KATELLO_CLI_LOGLEVEL"]) except (KeyError, ValueError): level = logging.INFO path = logfile() handler = RotatingFileHandler(path, maxBytes=0x100000, backupCount=5) handler.setFormatter(Formatter(FMT)) root.setLevel(level) root.addHandler(handler) log = logging.getLogger(name) return log
def log_collection_context(logging_memory_handler, tempdir, prefix, timestamp, output_path=None, creation_dir=None): from logging import root, DEBUG path = get_tar_path(prefix, output_path, timestamp, creation_dir) root.addHandler(logging_memory_handler) root.setLevel(DEBUG) try: yield path finally: with open_archive(path) as archive: root.removeHandler(logging_memory_handler) logging_memory_handler.flush() logging_memory_handler.close() add_directory(archive, tempdir) print("Logs collected successfully to {}".format(path))
def basicConfig(**kwargs): if len(root.handlers) == 0: if 'program_name' in kwargs: setProgramName(kwargs['program_name']) if 'filename' in kwargs: setLogFilename(kwargs['filename']) filename = _logFilename if filename: mode = kwargs.get("filemode", "a") hdlr = FileHandler(filename, mode) else: stream = kwargs.get("stream") hdlr = StreamHandler(stream) fs = kwargs.get("format", BASIC_FORMAT) dfs = kwargs.get("datefmt", None) fmt = SilkscreenFormatter(fs, dfs) hdlr.setFormatter(fmt) root.addHandler(hdlr) level = kwargs.get("level") if level: root.setLevel(level)
def init_log(logfile="log.log", level="INFO", server_addr=None): if len(root.handlers) is 0: # root record all root.setLevel(0) fmt = "%(asctime)s %(name)s,line:%(lineno)d [%(levelname)s] %(message)s" fmter = Formatter(fmt=fmt) # display on screen s_handler = StreamHandler() s_handler.setLevel(level) s_handler.setFormatter(fmter) root.addHandler(s_handler) # write all levels to logfile f_handler = FileHandler(logfile) # f_handler.setLevel(0) f_handler.setFormatter(fmter) root.addHandler(f_handler) # TCP handler if server_addr is not None: t_handler = SocketHandler(*server_addr) # t_handler.setLevel(0) t_handler.setFormatter(fmter) root.addHandler(t_handler) else: raise RuntimeError("init_debug() can only call once.")
def configure_logging(logger_name, filename=None): """ Configure logging and return the named logger and the location of the logging configuration file loaded. This function expects a Splunk app directory structure:: <app-root> bin ... default ... local ... This function looks for a logging configuration file at each of these locations, loading the first, if any, logging configuration file that it finds:: local/{name}.logging.conf default/{name}.logging.conf local/logging.conf default/logging.conf The current working directory is set to *<app-root>* before the logging configuration file is loaded. Hence, paths in the logging configuration file are relative to *<app-root>*. The current directory is reset before return. You may short circuit the search for a logging configuration file by providing an alternative file location in `path`. Logging configuration files must be in `ConfigParser format`_. #Arguments: :param logger_name: Logger name :type logger_name: bytes, unicode :param filename: Location of an alternative logging configuration file or `None`. :type filename: bytes, unicode or NoneType :returns: The named logger and the location of the logging configuration file loaded. :rtype: tuple .. _ConfigParser format: http://goo.gl/K6edZ8 """ if filename is None: if logger_name is None: probing_paths = [path.join('local', 'logging.conf'), path.join('default', 'logging.conf')] else: probing_paths = [ path.join('local', logger_name + '.logging.conf'), path.join('default', logger_name + '.logging.conf'), path.join('local', 'logging.conf'), path.join('default', 'logging.conf')] for relative_path in probing_paths: configuration_file = path.join(app_root, relative_path) if path.exists(configuration_file): filename = configuration_file break elif not path.isabs(filename): found = False for conf in 'local', 'default': configuration_file = path.join(app_root, conf, filename) if path.exists(configuration_file): filename = configuration_file found = True break if not found: raise ValueError('Logging configuration file "{}" not found in local or default directory'.format(filename)) elif not path.exists(filename): raise ValueError('Logging configuration file "{}" not found'.format(filename)) if filename is not None: global _current_logging_configuration_file filename = path.realpath(filename) if filename != _current_logging_configuration_file: working_directory = getcwdu() chdir(app_root) try: fileConfig(filename, {'SPLUNK_HOME': splunk_home}) finally: chdir(working_directory) _current_logging_configuration_file = filename if len(root.handlers) == 0: root.addHandler(StreamHandler()) return None if logger_name is None else getLogger(logger_name), filename
self.handleError(record) def close(self): self.acquire() try: if self.stream: self.flush() self.stream.close() self.stream = None Handler.close(self) finally: self.release() def doRollover(self): if self.stream: self.stream.close() self.stream = None self.baseFilename = self._pathnameprefix + '-' + \ time.strftime(self._dayfmt) + '.log' self.rollover_at += SECONDS_PER_DAY handler = StreamHandler() fmter = ConsoleFormatter(colorize=colorize) handler.setFormatter(fmter) root.addHandler(handler) loghandler = LogfileHandler(path.expanduser('~/.config/ufit')) loghandler.setLevel(WARNING) root.addHandler(loghandler) root.setLevel(INFO)
def configure_logging(logger_name, filename=None): """ Configure logging and return the named logger and the location of the logging configuration file loaded. This function expects a Splunk app directory structure:: <app-root> bin ... default ... local ... This function looks for a logging configuration file at each of these locations, loading the first, if any, logging configuration file that it finds:: local/{name}.logging.conf default/{name}.logging.conf local/logging.conf default/logging.conf The current working directory is set to *<app-root>* before the logging configuration file is loaded. Hence, paths in the logging configuration file are relative to *<app-root>*. The current directory is reset before return. You may short circuit the search for a logging configuration file by providing an alternative file location in `path`. Logging configuration files must be in `ConfigParser format`_. #Arguments: :param logger_name: Logger name :type logger_name: bytes, unicode :param filename: Location of an alternative logging configuration file or `None`. :type filename: bytes, unicode or NoneType :returns: The named logger and the location of the logging configuration file loaded. :rtype: tuple .. _ConfigParser format: http://goo.gl/K6edZ8 """ if filename is None: if logger_name is None: probing_paths = [path.join("local", "logging.conf"), path.join("default", "logging.conf")] else: probing_paths = [ path.join("local", logger_name + ".logging.conf"), path.join("default", logger_name + ".logging.conf"), path.join("local", "logging.conf"), path.join("default", "logging.conf"), ] for relative_path in probing_paths: configuration_file = path.join(app_root, relative_path) if path.exists(configuration_file): filename = configuration_file break elif not path.isabs(filename): found = False for conf in "local", "default": configuration_file = path.join(app_root, conf, filename) if path.exists(configuration_file): filename = configuration_file found = True break if not found: raise ValueError('Logging configuration file "{}" not found in local or default directory'.format(filename)) elif not path.exists(filename): raise ValueError('Logging configuration file "{}" not found'.format(filename)) if filename is not None: global _current_logging_configuration_file filename = path.realpath(filename) if filename != _current_logging_configuration_file: working_directory = getcwdu() chdir(app_root) try: fileConfig(filename, {"SPLUNK_HOME": splunk_home}) finally: chdir(working_directory) _current_logging_configuration_file = filename if len(root.handlers) == 0: root.addHandler(StreamHandler()) return None if logger_name is None else getLogger(logger_name), filename
def configure(name, probing_path=None, app_root=None): """ Configure logging and return a logger and the location of its logging configuration file. This function expects: + A Splunk app directory structure:: <app-root> bin ... default ... local ... + The current working directory is *<app-root>***/bin**. Splunk guarantees this. If you are running the app outside of Splunk, be sure to set the current working directory to *<app-root>***/bin** before calling. This function looks for a logging configuration file at each of these locations, loading the first, if any, logging configuration file that it finds:: local/{name}.logging.conf default/{name}.logging.conf local/logging.conf default/logging.conf The current working directory is set to *<app-root>* before the logging configuration file is loaded. Hence, paths in the logging configuration file are relative to *<app-root>*. The current directory is reset before return. You may short circuit the search for a logging configuration file by providing an alternative file location in `probing_path`. Logging configuration files must be in `ConfigParser format`_. #Arguments: :param name: Logger name :type name: str :param probing_path: Location of an alternative logging configuration file or `None` :type probing_path: str or NoneType :returns: A logger and the location of its logging configuration file :param app_root: The root of the application directory, used primarily by tests. :type app_root: str or NoneType .. _ConfigParser format: http://goo.gl/K6edZ8 """ app_directory = get_app_directory( sys.argv[0]) if app_root is None else app_root if probing_path is None: probing_paths = [ 'local/%s.logging.conf' % name, 'default/%s.logging.conf' % name, 'local/logging.conf', 'default/logging.conf' ] for relative_path in probing_paths: configuration_file = os.path.join(app_directory, relative_path) if os.path.exists(configuration_file): probing_path = configuration_file break elif not os.path.isabs(probing_path): found = False for conf in 'local', 'default': configuration_file = os.path.join(app_directory, conf, probing_path) if os.path.exists(configuration_file): probing_path = configuration_file found = True break if not found: raise ValueError( 'Logging configuration file "%s" not found in local or default ' 'directory' % probing_path) elif not os.path.exists(probing_path): raise ValueError('Logging configuration file "%s" not found') if probing_path is not None: working_directory = os.getcwd() os.chdir(app_directory) try: splunk_home = os.path.normpath( os.path.join(working_directory, os.environ['SPLUNK_HOME'])) except KeyError: splunk_home = working_directory # reasonable in debug scenarios try: probing_path = os.path.abspath(probing_path) fileConfig(probing_path, {'SPLUNK_HOME': splunk_home}) finally: os.chdir(working_directory) if len(root.handlers) == 0: root.addHandler(StreamHandler()) logger = getLogger(name) return logger, probing_path
def configure(name, probing_path=None, app_root=None): """ Configure logging and return a logger and the location of its logging configuration file. This function expects: + A Splunk app directory structure:: <app-root> bin ... default ... local ... + The current working directory is *<app-root>***/bin**. Splunk guarantees this. If you are running the app outside of Splunk, be sure to set the current working directory to *<app-root>***/bin** before calling. This function looks for a logging configuration file at each of these locations, loading the first, if any, logging configuration file that it finds:: local/{name}.logging.conf default/{name}.logging.conf local/logging.conf default/logging.conf The current working directory is set to *<app-root>* before the logging configuration file is loaded. Hence, paths in the logging configuration file are relative to *<app-root>*. The current directory is reset before return. You may short circuit the search for a logging configuration file by providing an alternative file location in `probing_path`. Logging configuration files must be in `ConfigParser format`_. #Arguments: :param name: Logger name :type name: str :param probing_path: Location of an alternative logging configuration file or `None` :type probing_path: str or NoneType :returns: A logger and the location of its logging configuration file :param app_root: The root of the application directory, used primarily by tests. :type app_root: str or NoneType .. _ConfigParser format: http://goo.gl/K6edZ8 """ app_directory = get_app_directory(sys.argv[0]) if app_root is None else app_root if probing_path is None: probing_paths = [ 'local/%s.logging.conf' % name, 'default/%s.logging.conf' % name, 'local/logging.conf', 'default/logging.conf'] for relative_path in probing_paths: configuration_file = os.path.join(app_directory, relative_path) if os.path.exists(configuration_file): probing_path = configuration_file break elif not os.path.isabs(probing_path): found = False for conf in 'local', 'default': configuration_file = os.path.join(app_directory, conf, probing_path) if os.path.exists(configuration_file): probing_path = configuration_file found = True break if not found: raise ValueError( 'Logging configuration file "%s" not found in local or default ' 'directory' % probing_path) elif not os.path.exists(probing_path): raise ValueError('Logging configuration file "%s" not found') if probing_path is not None: working_directory = os.getcwd() os.chdir(app_directory) try: splunk_home = os.path.normpath(os.path.join(working_directory, os.environ['SPLUNK_HOME'])) except KeyError: splunk_home = working_directory # reasonable in debug scenarios try: probing_path = os.path.abspath(probing_path) fileConfig(probing_path, {'SPLUNK_HOME': splunk_home}) finally: os.chdir(working_directory) if len(root.handlers) == 0: root.addHandler(StreamHandler()) logger = getLogger(name) return logger, probing_path