Python logging.handlers.WatchedFileHandler() Examples

The following are 12 code examples of logging.handlers.WatchedFileHandler(). 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.handlers , or try the search function .
Example #1
Source File: logger.py    From Dollynator with GNU Lesser General Public License v3.0 7 votes vote down vote up
def _get_logger(name=settings.logger_filename()):
    logger = logging.getLogger(name)

    if not logger.handlers:

        logger.setLevel(logging.INFO)

        # create formatter and handler
        formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        path = settings.logger_file()
        handler = WatchedFileHandler(path)
        # combine
        handler.setLevel(logging.INFO)
        handler.setFormatter(formatter)
        logger.addHandler(handler)

    return logger 
Example #2
Source File: logging.py    From ee-outliers with GNU General Public License v3.0 6 votes vote down vote up
def add_file_handler(self, log_file):
        """
        Create, format & add the handler that will log to the log file
        """
        handler = WatchedFileHandler(log_file)
        handler.setLevel(self.logger.level)
        formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s', "%Y-%m-%d %H:%M:%S")
        handler.setFormatter(formatter)
        self.logger.addHandler(handler) 
Example #3
Source File: application.py    From doorman with MIT License 6 votes vote down vote up
def register_loggers(app):
    if app.debug:
        return

    import logging
    from logging.handlers import WatchedFileHandler
    import sys

    logfile = app.config['DOORMAN_LOGGING_FILENAME']
    if logfile == '-':
        handler = logging.StreamHandler(sys.stdout)
    else:
        handler = WatchedFileHandler(logfile)
    levelname = app.config['DOORMAN_LOGGING_LEVEL']
    if levelname in ('DEBUG', 'INFO', 'WARN', 'WARNING', 'ERROR', 'CRITICAL'):
        handler.setLevel(getattr(logging, levelname))
    formatter = logging.Formatter(app.config['DOORMAN_LOGGING_FORMAT'])
    handler.setFormatter(formatter)

    app.logger.addHandler(handler) 
Example #4
Source File: valve_util.py    From faucet with Apache License 2.0 6 votes vote down vote up
def get_logger(logname, logfile, loglevel, propagate):
    """Create and return a logger object."""

    stream_handlers = {
        'STDOUT': sys.stdout,
        'STDERR': sys.stderr,
    }

    try:
        if logfile in stream_handlers:
            logger_handler = logging.StreamHandler(stream_handlers[logfile])
        else:
            logger_handler = WatchedFileHandler(logfile)
    except (PermissionError, FileNotFoundError) as err: # pytype: disable=name-error
        print(err)
        sys.exit(-1)

    logger = logging.getLogger(logname)
    log_fmt = '%(asctime)s %(name)-6s %(levelname)-8s %(message)s'
    logger_handler.setFormatter(
        logging.Formatter(log_fmt, '%b %d %H:%M:%S'))
    logger.addHandler(logger_handler)
    logger.propagate = propagate
    logger.setLevel(loglevel)
    return logger 
Example #5
Source File: grpc_shell.py    From SROS-grpc-services with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_logger(target='std'):
    global logger
    for h in logger.handlers[:]:
        logger.removeHandler(h)
    logger.setLevel(logging.DEBUG)
    formatter = logging.Formatter('\n%(asctime)s - %(name)s - %(levelname)s - \n%(message)s\n')
    if target == 'std':
        handler = logging.StreamHandler(sys.stdout)
    else:
        handler = WatchedFileHandler(target)
    handler.setFormatter(formatter)
    handler.setLevel(logging.DEBUG)
    logger.addHandler(handler) 
Example #6
Source File: finder.py    From influxgraph with Apache License 2.0 5 votes vote down vote up
def _setup_logger(self, level, log_file):
        """Setup log level and log file if set"""
        if not level:
            return
        if logger.handlers:
            return
        if hasattr(logging, 'NullHandler'):
            logger.addHandler(logging.NullHandler())
        formatter = logging.Formatter(
            '[%(levelname)s] %(asctime)s - %(module)s.%(funcName)s() '
            '- %(message)s')
        level = getattr(logging, level.upper())
        logger.setLevel(level)
        handler = logging.StreamHandler()
        logger.addHandler(handler)
        handler.setFormatter(formatter)
        if not log_file:
            return
        try:
            _handler = WatchedFileHandler(log_file)
        except IOError:
            logger.error("Could not write to %s, falling back to stdout",
                         log_file)
        else:
            logger.addHandler(_handler)
            _handler.setFormatter(formatter) 
Example #7
Source File: tracker_bot.py    From Dollynator with GNU Lesser General Public License v3.0 5 votes vote down vote up
def get_logger(self, name):
        logger = logging.getLogger(name)

        if not logger.handlers:
            logger.setLevel(logging.INFO)

            # create formatter and handler
            formatter = logging.Formatter('%(asctime)s;%(message)s')
            handler = WatchedFileHandler(os.path.join(log_file_path, name))
            # combine
            handler.setLevel(logging.INFO)
            handler.setFormatter(formatter)
            logger.addHandler(handler)

        return logger 
Example #8
Source File: logs.py    From ansibullbot with GNU General Public License v3.0 5 votes vote down vote up
def set_logger(debug=False, logfile=None):

    logFormatter = \
            logging.Formatter("%(asctime)s %(levelname)s %(process)d %(filename)s:%(funcName)s:%(lineno)d %(message)s")
    rootLogger = logging.getLogger()

    if debug:
        logging.level = logging.DEBUG
        rootLogger.setLevel(logging.DEBUG)
    else:
        logging.level = logging.INFO
        rootLogger.setLevel(logging.INFO)

    if logfile:
        try:
            logdir = os.path.dirname(logfile)
            if logdir and not os.path.isdir(logdir):
                os.makedirs(logdir)
            fileHandler = WatchedFileHandler(logfile)
            fileHandler.setFormatter(logFormatter)
            rootLogger.addHandler(fileHandler)
        except Exception as e:
            pass

    consoleHandler = logging.StreamHandler()
    consoleHandler.setFormatter(logFormatter)
    rootLogger.addHandler(consoleHandler) 
Example #9
Source File: logger.py    From graham_discord_bot with MIT License 5 votes vote down vote up
def setup_logger(log_file: str, log_level: int = logging.INFO) -> logging.Logger:
    root = logging.getLogger()
    logging.basicConfig(level=log_level)
    handler = WatchedFileHandler(log_file)
    formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s", "%Y-%m-%d %H:%M:%S %z")
    handler.setFormatter(formatter)
    root.addHandler(handler)
    root.addHandler(TimedRotatingFileHandler(log_file, when="d", interval=1, backupCount=100)) 
Example #10
Source File: logger.py    From nano-dpow with MIT License 5 votes vote down vote up
def get_logger():
    logger = logging.getLogger("dpow")
    logging.basicConfig(level=logging.INFO)
    log_file = "/tmp/dpow.txt"
    formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(filename)s@%(funcName)s:%(lineno)s", "%Y-%m-%d %H:%M:%S %z")
    handler = WatchedFileHandler(log_file)
    handler.setFormatter(formatter)
    logger.addHandler(handler)
    logger.addHandler(TimedRotatingFileHandler(log_file, when="d", interval=1, backupCount=100))
    return logger 
Example #11
Source File: natriumcast.py    From natrium-wallet-server with MIT License 4 votes vote down vote up
def init_app():
    """ Initialize the main application instance and return it"""
    async def close_redis(app):
        """Close redis connections"""
        log.server_logger.info('Closing redis connections')
        app['rdata'].close()

    async def open_redis(app):
        """Open redis connections"""
        log.server_logger.info("Opening redis connections")
        app['rdata'] = await aioredis.create_redis_pool((redis_host, redis_port),
                                                db=int(os.getenv('REDIS_DB', '2')), encoding='utf-8', minsize=2, maxsize=15)
        # Global vars
        app['clients'] = {} # Keep track of connected clients
        app['last_msg'] = {} # Last time a client has sent a message
        app['active_messages'] = set() # Avoid duplicate messages from being processes simultaneously
        app['cur_prefs'] = {} # Client currency preferences
        app['subscriptions'] = {} # Store subscription UUIDs, this is used for targeting callback accounts
        app['active_work'] = set() # Keep track of active work requests to prevent duplicates

    # Setup logger
    if debug_mode:
        logging.basicConfig(level=logging.DEBUG)
    else:
        root = logging.getLogger('aiohttp.server')
        logging.basicConfig(level=logging.INFO)
        if options.log_to_stdout:
            handler = logging.StreamHandler(sys.stdout)
            formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s", "%Y-%m-%d %H:%M:%S %z")
            handler.setFormatter(formatter)
            root.addHandler(handler)
        else:
            handler = WatchedFileHandler(log_file)
            formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s", "%Y-%m-%d %H:%M:%S %z")
            handler.setFormatter(formatter)
            root.addHandler(handler)
            root.addHandler(TimedRotatingFileHandler(log_file, when="d", interval=1, backupCount=100))        

    app = web.Application()
    cors = aiohttp_cors.setup(app, defaults={
        "*": aiohttp_cors.ResourceOptions(
                allow_credentials=True,
                expose_headers="*",
                allow_headers="*",
            )
    })    
    app.add_routes([web.get('/', websocket_handler)]) # All WS requests
    app.add_routes([web.post('/callback', callback)]) # HTTP Callback from node
    # HTTP API
    users_resource = cors.add(app.router.add_resource("/api"))
    cors.add(users_resource.add_route("POST", http_api))    
    #app.add_routes([web.post('/callback', callback)])
    app.on_startup.append(open_redis)
    app.on_shutdown.append(close_redis)

    return app 
Example #12
Source File: log.py    From snmpfwd with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def init(self, *priv):
        if not priv:
            raise SnmpfwdError('Bad log file params, need filename')
        if sys.platform[:3] == 'win':
            # fix possibly corrupted absolute windows path
            if len(priv[0]) == 1 and priv[0].isalpha() and len(priv) > 1:
                priv = [priv[0] + ':' + priv[1]] + list(priv[2:])

        maxsize = 0
        maxage = None
        if len(priv) > 1 and priv[1]:
            try:
                if priv[1][-1] == 'k':
                    maxsize = int(priv[1][:-1]) * 1024
                elif priv[1][-1] == 'm':
                    maxsize = int(priv[1][:-1]) * 1024 * 1024
                elif priv[1][-1] == 'g':
                    maxsize = int(priv[1][:-1]) * 1024 * 1024 * 1024
                elif priv[1][-1] == 'S':
                    maxage = ('S', int(priv[1][:-1]))
                elif priv[1][-1] == 'M':
                    maxage = ('M', int(priv[1][:-1]))
                elif priv[1][-1] == 'H':
                    maxage = ('H', int(priv[1][:-1]))
                elif priv[1][-1] == 'D':
                    maxage = ('D', int(priv[1][:-1]))
                else:
                    raise ValueError('Unknown log rotation criterion: %s' % priv[1][-1])

            except ValueError:
                raise SnmpfwdError(
                    'Error in timed log rotation specification. Use <NNN>k,m,g '
                    'for size or <NNN>S,M,H,D for time limits'
                )

        try:
            if maxsize:
                handler = handlers.RotatingFileHandler(priv[0], backupCount=30, maxBytes=maxsize)
            elif maxage:
                handler = self.TimedRotatingFileHandler(priv[0], backupCount=30, when=maxage[0], interval=maxage[1])
            else:
                handler = handlers.WatchedFileHandler(priv[0])

        except Exception:
            raise SnmpfwdError(
                'Failure configure logging: %s' % sys.exc_info()[1]
            )

        handler.setFormatter(logging.Formatter('%(message)s'))

        self._logger.addHandler(handler)

        self('Log file %s, rotation rules: %s' % (priv[0], maxsize and '> %sKB' % (maxsize/1024) or maxage and '%s%s' % (maxage[1], maxage[0]) or '<none>'))