Python logging.handlers() Examples

The following are 30 code examples of logging.handlers(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module logging , or try the search function .
Example #1
Source File: config.py    From circleci-demo-python-flask with MIT License 7 votes vote down vote up
def init_app(cls, app):
        Config.init_app(app)

        # email errors to the administrators
        import logging
        from logging.handlers import SMTPHandler
        credentials = None
        secure = None
        if getattr(cls, 'MAIL_USERNAME', None) is not None:
            credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
            if getattr(cls, 'MAIL_USE_TLS', None):
                secure = ()
        mail_handler = SMTPHandler(
            mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
            fromaddr=cls.CIRCULATE_MAIL_SENDER,
            toaddrs=[cls.CIRCULATE_ADMIN],
            subject=cls.CIRCULATE_MAIL_SUBJECT_PREFIX + ' Application Error',
            credentials=credentials,
            secure=secure)
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler) 
Example #2
Source File: config.py    From Simpleblog with MIT License 6 votes vote down vote up
def init_app(cls, app):
        Config.init_app(app)

        # 把错误发送管理员
        import logging
        from logging.handlers import SMTPHandler
        credentials = None
        secure = None
        if getattr(cls, 'MAIL_USERNAME', None) is not None:
            credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
            if getattr(cls, 'MAIL_USE_TLS', None):
                secure = ()
        mail_handler = SMTPHandler(
            mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
            fromaddr=cls.MAIL_SENDER,
            toaddrs=[cls.ADMINMAIL],
            subject=cls.MAIL_SUBJECT_PREFIX + ' Application Error',
            credentials=credentials,
            secure=secure)
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler) 
Example #3
Source File: config.py    From jawfish with MIT License 6 votes vote down vote up
def _handle_existing_loggers(existing, child_loggers, disable_existing):
    """
    When (re)configuring logging, handle loggers which were in the previous
    configuration but are not in the new configuration. There's no point
    deleting them as other threads may continue to hold references to them;
    and by disabling them, you stop them doing any logging.

    However, don't disable children of named loggers, as that's probably not
    what was intended by the user. Also, allow existing loggers to NOT be
    disabled if disable_existing is false.
    """
    root = logging.root
    for log in existing:
        logger = root.manager.loggerDict[log]
        if log in child_loggers:
            logger.level = logging.NOTSET
            logger.handlers = []
            logger.propagate = True
        else:
            logger.disabled = disable_existing 
Example #4
Source File: dictconfig.py    From jbox with MIT License 6 votes vote down vote up
def common_logger_config(self, logger, config, incremental=False):
        """
        Perform configuration which is common to root and non-root loggers.
        """
        level = config.get('level', None)
        if level is not None:
            logger.setLevel(_checkLevel(level))
        if not incremental:
            # Remove any existing handlers
            for h in logger.handlers[:]:
                logger.removeHandler(h)
            handlers = config.get('handlers', None)
            if handlers:
                self.add_handlers(logger, handlers)
            filters = config.get('filters', None)
            if filters:
                self.add_filters(logger, filters) 
Example #5
Source File: app.py    From leaguedirector with Apache License 2.0 6 votes vote down vote up
def setupLogging(self):
        logger = logging.getLogger()
        formatter = logging.Formatter('%(asctime)s [%(levelname)-8s] %(message)s')
        path = userpath('logs', 'leaguedirector.log')
        handler = logging.handlers.RotatingFileHandler(path, backupCount=20)
        try:
            handler.doRollover()
        except Exception: pass
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        handler = logging.StreamHandler()
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        logger.setLevel(logging.INFO)
        logging.info('Started League Director (%s)', leaguedirector.__version__)
        qInstallMessageHandler(self.handleMessage) 
Example #6
Source File: log_copy.py    From glazier with Apache License 2.0 6 votes vote down vote up
def _EventLogUpload(self, source_log: Text):
    """Upload the log file contents to the local EventLog."""
    event_handler = logging.handlers.NTEventLogHandler('GlazierBuildLog')
    logger = logging.Logger('eventlogger')
    logger.addHandler(event_handler)
    logger.setLevel(logging.DEBUG)

    try:
      with open(source_log, 'r') as f:
        content = f.readlines()
        for line in content:
          logger.info(line)
    except IOError:
      raise LogCopyError(
          'Unable to open log file. It will not be imported into '
          'the Windows Event Log.') 
Example #7
Source File: log.py    From knack with MIT License 6 votes vote down vote up
def configure(self, args):
        """ Configure the loggers with the appropriate log level etc.

        :param args: The arguments from the command line
        :type args: list
        """
        log_level = self._determine_log_level(args)
        log_level_config = self.console_log_configs[log_level]
        root_logger = logging.getLogger()
        cli_logger = logging.getLogger(CLI_LOGGER_NAME)
        # Set the levels of the loggers to lowest level.
        # Handlers can override by choosing a higher level.
        root_logger.setLevel(logging.DEBUG)
        cli_logger.setLevel(logging.DEBUG)
        cli_logger.propagate = False
        if root_logger.handlers and cli_logger.handlers:
            # loggers already configured
            return
        self._init_console_handlers(root_logger, cli_logger, log_level_config)
        if self.file_log_enabled:
            self._init_logfile_handlers(root_logger, cli_logger)
            get_logger(__name__).debug("File logging enabled - writing logs to '%s'.", self.log_dir) 
Example #8
Source File: cim_actions.py    From misp42splunk with GNU Lesser General Public License v3.0 6 votes vote down vote up
def setup_logger(name, level=logging.INFO, maxBytes=25000000, backupCount=5, format=SHORT_FORMAT):
        """ Set up a logging instance.

        @param name:        The log file name.
                            We recommend "$action_name$_modalert".
        @param level:       The logging level.
        @param maxBytes:    The maximum log file size before rollover.
        @param backupCount: The number of log files to retain.

        @return logger:     Returns an instance of logger
        """
        logfile = make_splunkhome_path(['var', 'log', 'splunk', name + '.log'])
        logger = logging.getLogger(name)
        logger.setLevel(level)
        logger.propagate = False  # Prevent the log messages from being duplicated in the python.log file

        # 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, maxBytes=maxBytes, backupCount=backupCount)
            formatter = logging.Formatter(format)
            file_handler.setFormatter(formatter)
            logger.addHandler(file_handler)

        return logger 
Example #9
Source File: log.py    From LagouJob with Apache License 2.0 6 votes vote down vote up
def add_handler(cls, level, fmt, colorful, **kwargs):
    """Add a configured handlers to the global logger."""
    global g_logger

    if isinstance(level, str):
        level = getattr(logging, level.upper(), logging.INFO)

    handler = cls(**kwargs)
    handler.setLevel(level)

    if colorful:
        formatter = ColoredFormatter(fmt)
    else:
        formatter = logging.Formatter(fmt)

    handler.setFormatter(formatter)
    g_logger.addHandler(handler)

    return handler 
Example #10
Source File: dictconfig.py    From recruit with Apache License 2.0 6 votes vote down vote up
def common_logger_config(self, logger, config, incremental=False):
        """
        Perform configuration which is common to root and non-root loggers.
        """
        level = config.get('level', None)
        if level is not None:
            logger.setLevel(_checkLevel(level))
        if not incremental:
            # Remove any existing handlers
            for h in logger.handlers[:]:
                logger.removeHandler(h)
            handlers = config.get('handlers', None)
            if handlers:
                self.add_handlers(logger, handlers)
            filters = config.get('filters', None)
            if filters:
                self.add_filters(logger, filters) 
Example #11
Source File: http.py    From tomodachi with MIT License 6 votes vote down vote up
def colorize_status(text: Optional[Union[str, int]], status: Optional[Union[str, int, bool]] = False) -> str:
        if status is False:
            status = text
        status_code = str(status) if status else None
        if status_code and not logging.getLogger('transport.http').handlers:
            output_text = str(text) if text else ''
            color = None

            if status_code == '101':
                color = colorama.Fore.CYAN
            elif status_code[0] == '2':
                color = colorama.Fore.GREEN
            elif status_code[0] == '3' or status_code == '499':
                color = colorama.Fore.YELLOW
            elif status_code[0] == '4':
                color = colorama.Fore.RED
            elif status_code[0] == '5':
                color = colorama.Fore.WHITE + colorama.Back.RED

            if color:
                return '{}{}{}'.format(color, output_text, colorama.Style.RESET_ALL)
            return output_text

        return str(text) if text else '' 
Example #12
Source File: config.py    From Flashcards with MIT License 6 votes vote down vote up
def init_app(cls, app):
        Config.init_app(app)

        import logging
        from logging.handlers import SMTPHandler
        credentials = None
        secure = None
        if getattr(cls, 'MAIL_USERNAME', None) is not None:
            credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
            if getattr(cls, 'MAIL_USE_TLS', None):
                secure = ()
        mail_handler = SMTPHandler(
            mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
            fromaddr=cls.FLASHCARD_MAIL_SENDER,
            toaddrs=[cls.FLASHCARD_ADMIN],
            subject=cls.FLASHCARD_MAIL_SUBJECT_PREFIX + 'Application Error',
            credentials=credentials,
            secure=secure)
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler) 
Example #13
Source File: autotune.py    From scVI with MIT License 5 votes vote down vote up
def _cleanup_logger():
    """Removes added handlers."""
    logger_all.debug("Cleaning up: removing added logging handler.")
    hp_logger = logging.getLogger("hyperopt")
    for handler in hp_logger.handlers:
        if handler == fh_hyperopt:
            logger_all.debug("Cleaning up: removing hyperopt FileHandler.")
            hp_logger.removeHandler(fh_hyperopt)
            break
    for handler in logger_all.handlers:
        if handler == fh_autotune:
            logger_all.debug("Cleaning up: removing autotune FileHandler.")
            logger_all.removeHandler(fh_autotune) 
Example #14
Source File: utils.py    From APIFuzzer with GNU General Public License v3.0 5 votes vote down vote up
def set_logger(level='warning', basic_output=False):
    fmt = '%(process)d [%(levelname)s] %(name)s: %(message)s'
    if basic_output:
        logging.basicConfig(format=fmt)
        logger = logging.getLogger()
    else:
        logger = logging.getLogger()
        if not len(logger.handlers):
            handler = logging.StreamHandler()
            if os.path.exists('/dev/log'):
                handler = SysLogHandler(address='/dev/log', facility=SysLogHandler.LOG_LOCAL2)
            handler.setFormatter(Formatter('%(process)d [%(levelname)s] %(name)s: %(message)s'))
            logger.addHandler(handler)
    logger.setLevel(level=level.upper())
    return logger 
Example #15
Source File: log.py    From LagouJob with Apache License 2.0 5 votes vote down vote up
def add_stream_handler(level, fmt):
    """Add a stream handlers to the global logger."""
    return add_handler(logging.StreamHandler, level, fmt, True) 
Example #16
Source File: mylog.py    From genmon with GNU General Public License v2.0 5 votes vote down vote up
def SetupLogger(logger_name, log_file, level=logging.INFO, stream = False):


    logger = logging.getLogger(logger_name)

    # remove existing logg handlers
    for handler in logger.handlers[:]:      # make a copy of the list
        logger.removeHandler(handler)

    logger.setLevel(level)

    formatter = logging.Formatter('%(asctime)s : %(message)s')

    if log_file != "":

        rotate = logging.handlers.RotatingFileHandler(log_file, mode='a',maxBytes=50000,backupCount=5)
        rotate.setFormatter(formatter)
        logger.addHandler(rotate)

    if stream:      # print to screen also?
        streamHandler = logging.StreamHandler()
        # Dont format stream log messages
        logger.addHandler(streamHandler)


    return logging.getLogger(logger_name) 
Example #17
Source File: rmLogging.py    From rainmachine-developer-resources with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, persistentFileName, filename, interval=86400, encoding=None,  delay=False):
            logging.handlers.BaseRotatingHandler.__init__(self, filename, 'w', encoding=encoding, delay=delay)
            self.interval = interval
            self.persistentFileName = persistentFileName

            self.lastRotate = int(time.time())
            if os.path.exists(persistentFileName):
                self.lastRotate = int(os.path.getmtime(persistentFileName)) 
Example #18
Source File: log.py    From LagouJob with Apache License 2.0 5 votes vote down vote up
def add_file_handler(level, fmt, filename, mode, backup_count, limit, when):
    """Add a file handlers to the global logger."""
    kwargs = {}

    # If the filename is not set, use the default filename
    if filename is None:
        logs_directory = os.path.join(os.path.dirname(__file__), os.pardir) + "/logs"
        if os.path.isdir(logs_directory) is not True:
            os.mkdir(logs_directory)
        filename = logs_directory + os.sep + 'anal' + '.log'

    kwargs['filename'] = filename

    # Choose the file_handler based on the passed arguments
    if backup_count == 0:  # Use FileHandler
        cls = logging.FileHandler
        kwargs['mode'] = mode
    elif when is None:  # Use RotatingFileHandler
        cls = logging.handlers.RotatingFileHandler
        kwargs['maxBytes'] = limit
        kwargs['backupCount'] = backup_count
        kwargs['mode'] = mode
    else:  # Use TimedRotatingFileHandler
        cls = logging.handlers.TimedRotatingFileHandler
        kwargs['when'] = when
        kwargs['interval'] = 1  # 1个单位
        kwargs['backupCount'] = backup_count

    return add_handler(cls, level, fmt, False, **kwargs) 
Example #19
Source File: AnsibleApi_v29.py    From AnsibleUI with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, task_id):
        super().__init__()
        self.id = task_id
        self.results = []
        self.r = redis.Redis(host=REDIS_ADDR, port=REDIS_PORT, password=REDIS_PD, db=ansible_result_redis_db)
        self.log = logging.getLogger('AnsibleApiLog')
        self.log.propagate = False
        spath = logging.handlers.RotatingFileHandler("logs/ansible_api.log", "a", 0, 1)
        spath.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s"))
        self.log.addHandler(spath)
        self.log.setLevel(logging.DEBUG) 
Example #20
Source File: manage.py    From Flask-Large-Application-Example with MIT License 5 votes vote down vote up
def setup_logging(name=None):
    """Setup Google-Style logging for the entire application.

    At first I hated this but I had to use it for work, and now I prefer it. Who knew?
    From: https://github.com/twitter/commons/blob/master/src/python/twitter/common/log/formatters/glog.py

    Always logs DEBUG statements somewhere.

    Positional arguments:
    name -- Append this string to the log file filename.
    """
    log_to_disk = False
    if OPTIONS['--log_dir']:
        if not os.path.isdir(OPTIONS['--log_dir']):
            print('ERROR: Directory {} does not exist.'.format(OPTIONS['--log_dir']))
            sys.exit(1)
        if not os.access(OPTIONS['--log_dir'], os.W_OK):
            print('ERROR: No permissions to write to directory {}.'.format(OPTIONS['--log_dir']))
            sys.exit(1)
        log_to_disk = True

    fmt = '%(levelletter)s%(asctime)s.%(msecs).03d %(process)d %(filename)s:%(lineno)d] %(message)s'
    datefmt = '%m%d %H:%M:%S'
    formatter = CustomFormatter(fmt, datefmt)

    console_handler = logging.StreamHandler(sys.stdout)
    console_handler.setLevel(logging.ERROR if log_to_disk else logging.DEBUG)
    console_handler.setFormatter(formatter)

    root = logging.getLogger()
    root.setLevel(logging.DEBUG)
    root.addHandler(console_handler)

    if log_to_disk:
        file_name = os.path.join(OPTIONS['--log_dir'], 'pypi_portal_{}.log'.format(name))
        file_handler = logging.handlers.TimedRotatingFileHandler(file_name, when='d', backupCount=7)
        file_handler.setFormatter(formatter)
        root.addHandler(file_handler) 
Example #21
Source File: dictconfig.py    From jbox with MIT License 5 votes vote down vote up
def add_handlers(self, logger, handlers):
        """Add handlers to a logger from a list of names."""
        for h in handlers:
            try:
                logger.addHandler(self.config['handlers'][h])
            except StandardError as e:
                raise ValueError('Unable to add handler %r: %s' % (h, e)) 
Example #22
Source File: base_learner.py    From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def create_logger(self, log_path=None):
        if log_path is None:
            return None
        check_and_create_dir(log_path)
        handler = logging.handlers.RotatingFileHandler(log_path, mode="a", maxBytes=100000000, backupCount=200)
        logging.root.removeHandler(absl.logging._absl_handler) # this removes duplicated logging
        absl.logging._warn_preinit_stderr = False # this removes duplicated logging
        formatter = RequestFormatter("[%(asctime)s] %(levelname)s: %(message)s")
        handler.setFormatter(formatter)
        logger = logging.getLogger(log_path)
        logger.setLevel(logging.INFO)
        for hdlr in logger.handlers[:]:
            logger.removeHandler(hdlr) # remove old handlers
        logger.addHandler(handler)
        self.logger = logger 
Example #23
Source File: logutils.py    From pydarkstar with MIT License 5 votes vote down vote up
def add_rotating_file_handler(level=logging.DEBUG, fname='app.log',
                              logger=None, fmt=lfmt, **kwargs):
    """
    Create rotating file handler and add it to logging.

    :param level: logging level
    :param fname: name of file
    :param logger: logger instance
    :param fmt: format
    """

    _kwargs = dict(maxBytes=(1048576 * 5), backupCount=5)
    _kwargs.update(**kwargs)

    handler = logging.handlers.RotatingFileHandler(fname, **kwargs)
    handler.setLevel(level)

    formatter = logging.Formatter(fmt)
    handler.setFormatter(formatter)

    if isinstance(logger, str):
        logger = logging.getLogger(logger)

    elif logger is None:
        logger = logging.getLogger()

    logger.addHandler(handler)

    return logger 
Example #24
Source File: rmLogging.py    From rainmachine-developer-resources with GNU General Public License v3.0 5 votes vote down vote up
def enableFileLogging(self, fileName = "log/rainmachine.log"):
        self._logFileName = fileName
        self.__checkAndCreateLogDir()
        try:

            if RMLogger.ENABLE_COMPRESSION:
                fileHandler = RMLogger.CompressingRotatingFileHandler(fileName, maxBytes=RMLogger.ROTATE_FILE_SIZE, backupCount=1)
            else:
                fileHandler = logging.handlers.RotatingFileHandler(fileName, maxBytes=RMLogger.ROTATE_FILE_SIZE, backupCount=1)

            fileHandler.setFormatter(self.format)
            self.logger.addHandler(fileHandler)

        except Exception, e:
            self.logger.error("Cannot enable file logging to %s: %s" % (fileName, e)) 
Example #25
Source File: mail_logging.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def get_mail_handler(smtp_server, mail_admin, from_email):
    """ Set up the handler sending emails for big exception
    """

    mail_handler = logging.handlers.SMTPHandler(
        smtp_server, from_email, mail_admin, "Pagure error"
    )
    mail_handler.setFormatter(logging.Formatter(MSG_FORMAT))
    mail_handler.setLevel(logging.ERROR)
    mail_handler.addFilter(ContextInjector())
    return mail_handler 
Example #26
Source File: log.py    From knack with MIT License 5 votes vote down vote up
def _init_logfile_handlers(self, root_logger, cli_logger):
        ensure_dir(self.log_dir)
        log_file_path = os.path.join(self.log_dir, self.logfile_name)
        from logging.handlers import RotatingFileHandler
        logfile_handler = RotatingFileHandler(log_file_path, maxBytes=10 * 1024 * 1024, backupCount=5)
        lfmt = logging.Formatter('%(process)d : %(asctime)s : %(levelname)s : %(name)s : %(message)s')
        logfile_handler.setFormatter(lfmt)
        logfile_handler.setLevel(logging.DEBUG)
        root_logger.addHandler(logfile_handler)
        cli_logger.addHandler(logfile_handler) 
Example #27
Source File: logging.py    From recruit with Apache License 2.0 5 votes vote down vote up
def _open(self):
        ensure_dir(os.path.dirname(self.baseFilename))
        return logging.handlers.RotatingFileHandler._open(self) 
Example #28
Source File: dictconfig.py    From recruit with Apache License 2.0 5 votes vote down vote up
def add_handlers(self, logger, handlers):
        """Add handlers to a logger from a list of names."""
        for h in handlers:
            try:
                logger.addHandler(self.config['handlers'][h])
            except StandardError as e:
                raise ValueError('Unable to add handler %r: %s' % (h, e)) 
Example #29
Source File: logging.py    From tomodachi with MIT License 5 votes vote down vote up
def log_setup(service: Any, name: Optional[str] = None, level: Optional[Union[str, int]] = None, formatter: Optional[Union[logging.Formatter, str, bool]] = True, filename: Optional[str] = None) -> logging.Logger:
    if not name:
        name = 'log.{}'.format(service.name)
    if not filename:
        raise Exception('log_filename must be specified for logging setup')

    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)

    if level and type(level) is str:
        level = getattr(logging, str(level))

    if not [x for x in logger.handlers if isinstance(x, CustomServiceLogHandler) and (level is None or level == x.level)]:
        try:
            wfh = CustomServiceLogHandler(filename=filename)
        except FileNotFoundError as e:
            logging.getLogger('logging').warning('Unable to use file for logging - invalid path ("{}")'.format(filename))
            raise e
        except PermissionError as e:
            logging.getLogger('logging').warning('Unable to use file for logging - invalid permissions ("{}")'.format(filename))
            raise e

        if level:
            wfh.setLevel(level)

        if formatter and type(formatter) is str:
            formatter = logging.Formatter(str(formatter))
        if formatter and type(formatter) is bool and formatter is True:
            formatter = logging.Formatter('%(asctime)s (%(name)s): %(message)s')

        if formatter and isinstance(formatter, logging.Formatter):
            wfh.setFormatter(formatter)

        logger.addHandler(wfh)

    return logger 
Example #30
Source File: _logging.py    From python-zhmcclient with Apache License 2.0 5 votes vote down vote up
def get_logger(name):
    """
    Return a :class:`~py:logging.Logger` object with the specified name.

    A :class:`~py:logging.NullHandler` handler is added to the logger if it
    does not have any handlers yet and if it is not the Python root logger.
    This prevents the propagation of log requests up the Python logger
    hierarchy, and therefore causes this package to be silent by default.
    """
    logger = logging.getLogger(name)
    if name != '' and not logger.handlers:
        logger.addHandler(logging.NullHandler())
    return logger