本文整理汇总了Python中logging.handlers.WatchedFileHandler方法的典型用法代码示例。如果您正苦于以下问题:Python handlers.WatchedFileHandler方法的具体用法?Python handlers.WatchedFileHandler怎么用?Python handlers.WatchedFileHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类logging.handlers
的用法示例。
在下文中一共展示了handlers.WatchedFileHandler方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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
示例2: register_loggers
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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)
示例3: get_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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
示例4: add_file_handler
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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)
示例5: set_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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)
示例6: _setup_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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)
示例7: get_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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
示例8: set_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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)
示例9: setup_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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))
示例10: get_logger
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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
示例11: init_app
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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
示例12: init
# 需要导入模块: from logging import handlers [as 别名]
# 或者: from logging.handlers import WatchedFileHandler [as 别名]
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>'))