def init_app(cls, app):
ProductionConfig.init_app(app)
# handle proxy server headers
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
## log to syslog
# write to /var/log/messages
# can be configured to write to a separate log file
# see docs
import logging
from logging.handlers import SysLogHandler
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.WARNING)
app.logger.addHandler(syslog_handler)
python类SysLogHandler()的实例源码
def init(self, *priv):
if len(priv) < 1:
raise error.SnmpsimError('Bad syslog params, need at least facility, also accept priority, host, port, socktype (tcp|udp)')
if len(priv) < 2:
priv = [priv[0], 'debug']
if len(priv) < 3:
priv = [priv[0], priv[1], 'localhost', 514, 'udp']
if not priv[2].startswith('/'):
if len(priv) < 4:
priv = [priv[0], priv[1], priv[2], 514, 'udp']
if len(priv) < 5:
priv = [priv[0], priv[1], priv[2], 514, 'udp']
priv = [priv[0], priv[1], priv[2], int(priv[3]), priv[4]]
try:
handler = handlers.SysLogHandler(priv[2].startswith('/') and priv[2] or (priv[2], int(priv[3])),
priv[0].lower(), len(priv) > 4 and priv[
4] == 'tcp' and socket.SOCK_STREAM or socket.SOCK_DGRAM)
except:
raise error.SnmpsimError('Bad syslog option(s): %s' % sys.exc_info()[1])
handler.setFormatter(logging.Formatter('%(asctime)s %(name)s: %(message)s'))
self._logger.addHandler(handler)
def _setup_logging(self, args, default_log_level):
fmt = '%(asctime)s [%(thread)d] %(levelname)-5s %(name)s %(message)s'
formatter = Formatter(fmt=fmt, datefmt='%Y-%m-%dT%H:%M:%S ')
stream = stdout if args.log_stream_stdout else stderr
handler = StreamHandler(stream=stream)
handler.setFormatter(formatter)
logger = getLogger()
logger.addHandler(handler)
if args.log_syslog:
fmt = 'octodns[%(process)-5s:%(thread)d]: %(name)s ' \
'%(levelname)-5s %(message)s'
handler = SysLogHandler(address=args.syslog_device,
facility=args.syslog_facility)
handler.setFormatter(Formatter(fmt=fmt))
logger.addHandler(handler)
logger.level = DEBUG if args.debug else default_log_level
# boto is noisy, set it to warn
getLogger('botocore').level = WARN
# DynectSession is noisy too
getLogger('DynectSession').level = WARN
def setup(self, socket, address, facility):
if socket == "file":
socktype = None
elif socket == "udp":
socktype = SOCK_DGRAM
elif socket == "tcp":
socktype = SOCK_STREAM
hdlr = SysLogHandler(
address=address,
facility=facility,
socktype=socktype
)
hdlr.setLevel(1)
fmt = logging.Formatter(
"RIPEAtlasMonitor[%(process)d] %(message)s"
)
hdlr.setFormatter(fmt)
self.logger.addHandler(hdlr)
def setup_logging(logger, syslog_socket, log_format, log_level='DEBUG'):
log_level = log_level.upper()
if log_level not in ['CRITICAL', 'ERROR', 'WARNING',
'INFO', 'DEBUG', 'NOTSET']:
raise Exception('Invalid log level: {}'.format(log_level.upper()))
logger.setLevel(getattr(logging, log_level))
formatter = logging.Formatter(log_format)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(formatter)
logger.addHandler(consoleHandler)
if syslog_socket != '/dev/null':
syslogHandler = SysLogHandler(syslog_socket)
syslogHandler.setFormatter(formatter)
logger.addHandler(syslogHandler)
def init_app(cls, app):
ProductionConfig.init_app(app)
# log to syslog
import logging
from logging.handlers import SysLogHandler
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.WARNING)
app.logger.addHandler(syslog_handler)
def __syslog_handler():
# type: () -> logging.Handler
osfamily = Facter.get(OsFamily)
if osfamily in (OsFamily.Debian, OsFamily.RedHat, OsFamily.Suse):
try:
handler = SysLogHandler(address='/dev/log')
except EnvironmentError:
handler = SysLogHandler()
else:
handler = SysLogHandler()
fmt = puppeter.__program__ + '[%(process)d]: %(levelname)s %(name)s - %(message)s'
handler.setFormatter(logging.Formatter(fmt=fmt))
handler.setLevel(logging.INFO)
return handler
def create_logger(logger_name):
# create logger
logger = logging.getLogger(logger_name)
logger.log_except = method_type(log_traceback_hook, logger) # add a method to logger
logger.setLevel(LoggerManager.log_level)
# create handler
if LoggerManager.log_handler == HANDLER_SYSLOG:
if platform.system() == 'Linux':
handler = LH.SysLogHandler(SYSLOG_ADDRESS, facility=SYSLOG_FACILITY)
else: # Windows, Mac
handler = logging.FileHandler(LoggerManager.get_filename(), encoding='utf8')
elif LoggerManager.log_handler == HANDLER_FILE:
handler = logging.FileHandler(LoggerManager.get_filename(), encoding='utf8')
elif LoggerManager.log_handler == HANDLER_CUSTOME:
handler = LoggerManager.custom_handler()
else:
handler = logging.StreamHandler()
# create formatter
fmt = FORMATTER.replace('%(tag)s', LoggerManager.log_tag)
formatter = logging.Formatter(fmt)
handler.setFormatter(formatter)
handler.setLevel(LoggerManager.log_level)
logger.addHandler(handler)
LoggerManager.created_loggers.add(logger_name)
def init_app(cls, app):
ProductionConfig.init_app(app)
# log to syslog
import logging
from logging.handlers import SysLogHandler
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.WARNING)
app.logger.addHandler(syslog_handler)
# configuration mapping for diffrent cases
def __init__(self, name, settings):
self.settings = settings
self._mailer = MailReporter(settings)
base_dir = os.path.dirname(os.path.dirname(
os.path.realpath(__file__)))
log_dir = os.path.join(base_dir, 'logs')
for logname in self.known_logs:
log = getLogger(logname)
log.setLevel(str_to_log_level(
settings['reporter.%s.log_level' % logname]))
output_type = settings['reporter.%s.type' % logname]
if output_type == 'syslog':
log.addHandler(SysLogHandler(address='/dev/log'))
elif output_type == 'file':
filename = settings['reporter.%s.filename' % logname]
if not filename:
filename = '%s-%s.log' % (name, logname)
elif os.path.dirname(filename) == '':
filename = os.path.join(log_dir, '%s-%s' % (name,
filename))
filesize = int(settings['reporter.%s.filesize' % logname])
filecount = int(settings['reporter.%s.filecount' % logname])
if filecount > 0:
filecount -= 1
log.addHandler(RotatingFileHandler(filename,
maxBytes=filesize, backupCount=filecount))
elif output_type == 'console':
log.addHandler(StreamHandler())
def configure_logging(options):
'''Configure log-level and logging handlers.
:param bool quiet:
If *True*, do not configure a console handler.
Defaults to *False*.
:param bool verbose:
If *True*, set the log-level for the console handler
to DEBUG. Has no effect if ``quiet`` is *True*.
Defaults to *False*.
:param str logfile:
If given, set up a RotatingFileHandler to receive logging output.
Should be the absolute path to the desired logfile
or special value "syslog".
Defaults to *None* (no logfile).
:param int log_level:
Level to use for ``logfile``.
Must be one of the constants defined in the ``logging`` module
(e.g. DEBUG, INFO, ...).
Has no effect if ``logfile`` is not given.
'''
rootlog = logging.getLogger()
rootlog.setLevel(logging.DEBUG)
if not options.quiet:
console_hdl = logging.StreamHandler()
console_level = logging.DEBUG if options.verbose else logging.WARNING
console_hdl.setLevel(console_level)
console_hdl.setFormatter(logging.Formatter(CONSOLE_FMT))
rootlog.addHandler(console_hdl)
if options.logfile:
if options.logfile == 'syslog':
logfile_hdl = handlers.SysLogHandler(address='/dev/log')
logfile_hdl.setFormatter(logging.Formatter(SYSLOG_FMT))
else:
logfile_hdl = handlers.RotatingFileHandler(options.logfile)
logfile_hdl.setFormatter(logging.Formatter(LOGFILE_FMT))
logfile_hdl.setLevel(options.log_level)
rootlog.addHandler(logfile_hdl)
def init_app(cls, app):
ProductionConfig.init_app(app)
# log to syslog
import logging
from logging.handlers import SysLogHandler
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.INFO)
app.logger.addHandler(syslog_handler)
def init_app(cls, app):
ProductionConfig.init_app(app)
# Log to syslog
import logging
from logging.handlers import SysLogHandler
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.WARNING)
app.logger.addHandler(syslog_handler)
def configure_syslog(request=None, logger=None, exceptions=False):
"""
Configure syslog logging channel.
It is turned on by setting `syslog_host` in the config file.
The port default to 514 can be overridden by setting `syslog_port`.
:param request: tornado.httputil.HTTPServerRequest instance
:param exceptions: boolean - This indicates if we should raise
exceptions encountered in the logging system.
"""
syslog_host = getattr(options, 'syslog_host', None)
if not syslog_host:
return
sys.modules["logging"].raiseExceptions = exceptions
handler = SysLogHandler(address=(syslog_host, options.syslog_port))
formatter = log_formatter(request)
handler.setFormatter(formatter)
if request:
handler.addFilter(RequestFilter(request))
if logger:
logger.addHandler(handler)
else:
logging.getLogger().addHandler(handler)
def init_app(cls, app):
ProductionConfig.init_app(app)
# log to syslog
import logging
from logging.handlers import SysLogHandler
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.WARNING)
app.logger.addHandler(syslog_handler)
def get_handler(debug=False, syslog=False, logfile=None, rotation_parameters=None):
"""get an apropriate handler according to given parameters"""
if os.environ.get('APYCOT_ROOT'):
handler = logging.StreamHandler(sys.stdout)
if debug:
handler = logging.StreamHandler()
elif logfile is None:
if syslog:
from logging import handlers
handler = handlers.SysLogHandler()
else:
handler = logging.StreamHandler()
else:
try:
if rotation_parameters is None:
if os.name == 'posix' and sys.version_info >= (2, 6):
from logging.handlers import WatchedFileHandler
handler = WatchedFileHandler(logfile)
else:
handler = logging.FileHandler(logfile)
else:
from logging.handlers import TimedRotatingFileHandler
handler = TimedRotatingFileHandler(
logfile, **rotation_parameters)
except IOError:
handler = logging.StreamHandler()
return handler
def get_handler(debug=False, syslog=False, logfile=None, rotation_parameters=None):
"""get an apropriate handler according to given parameters"""
if os.environ.get('APYCOT_ROOT'):
handler = logging.StreamHandler(sys.stdout)
if debug:
handler = logging.StreamHandler()
elif logfile is None:
if syslog:
from logging import handlers
handler = handlers.SysLogHandler()
else:
handler = logging.StreamHandler()
else:
try:
if rotation_parameters is None:
if os.name == 'posix' and sys.version_info >= (2, 6):
from logging.handlers import WatchedFileHandler
handler = WatchedFileHandler(logfile)
else:
handler = logging.FileHandler(logfile)
else:
from logging.handlers import TimedRotatingFileHandler
handler = TimedRotatingFileHandler(
logfile, **rotation_parameters)
except IOError:
handler = logging.StreamHandler()
return handler
def init_app(cls, app):
ProductionConfig.init_app(app)
# Log to syslog
import logging
from logging.handlers import SysLogHandler
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.WARNING)
app.logger.addHandler(syslog_handler)
def setup_custom_logger(name):
format_string = '%(asctime)s - %(levelname)s - %(module)s - %(message)s'
formatter = logging.Formatter(fmt=format_string)
#handler = SysLogHandler(address='/var/run/syslog', facility='local1')
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
logger.addHandler(handler)
return logger
def get_handler(debug=False, syslog=False, logfile=None, rotation_parameters=None):
"""get an apropriate handler according to given parameters"""
if os.environ.get('APYCOT_ROOT'):
handler = logging.StreamHandler(sys.stdout)
if debug:
handler = logging.StreamHandler()
elif logfile is None:
if syslog:
from logging import handlers
handler = handlers.SysLogHandler()
else:
handler = logging.StreamHandler()
else:
try:
if rotation_parameters is None:
if os.name == 'posix' and sys.version_info >= (2, 6):
from logging.handlers import WatchedFileHandler
handler = WatchedFileHandler(logfile)
else:
handler = logging.FileHandler(logfile)
else:
from logging.handlers import TimedRotatingFileHandler
handler = TimedRotatingFileHandler(
logfile, **rotation_parameters)
except IOError:
handler = logging.StreamHandler()
return handler
def init_app(cls, app):
ProductionConfig.init_app(app)
# log to syslog
import logging
from logging.handlers import SysLogHandler
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.WARNING)
app.logger.addHandler(syslog_handler)
#
def init_app(cls, app):
ProductionConfig.init_app(app)
import logging
from logging.handlers import SysLogHandler
syslog_handler = SysLogHandler()
syslog_handler.setLevel(logging.WARNING)
app.logger.addHandler(syslog_handler)
def get_handler(debug=False, syslog=False, logfile=None, rotation_parameters=None):
"""get an apropriate handler according to given parameters"""
if os.environ.get('APYCOT_ROOT'):
handler = logging.StreamHandler(sys.stdout)
if debug:
handler = logging.StreamHandler()
elif logfile is None:
if syslog:
from logging import handlers
handler = handlers.SysLogHandler()
else:
handler = logging.StreamHandler()
else:
try:
if rotation_parameters is None:
if os.name == 'posix' and sys.version_info >= (2, 6):
from logging.handlers import WatchedFileHandler
handler = WatchedFileHandler(logfile)
else:
handler = logging.FileHandler(logfile)
else:
from logging.handlers import TimedRotatingFileHandler
handler = TimedRotatingFileHandler(
logfile, **rotation_parameters)
except IOError:
handler = logging.StreamHandler()
return handler
def __init__(self, logger='cvprac', syslog=False, filename=None,
log_level='INFO'):
''' Initialize the client and configure logging. Either syslog, file
logging, both, or none can be enabled. If neither syslog
nor filename is specified then no logging will be performed.
Args:
logger (str): The name assigned to the logger.
syslog (bool): If True enable logging to syslog. Default is
False.
filename (str): Log to the file specified by filename. Default
is None.
log_level (str): Log level to use for logger. Default is INFO.
'''
self.authdata = None
self.cert = False
self.connect_timeout = None
self.cookies = None
self.error_msg = ''
self.node_cnt = None
self.node_pool = None
self.nodes = None
self.port = None
self.protocol = None
self.session = None
self.url_prefix = None
self._last_used_node = None
# Save proper headers
self.headers = {'Accept': 'application/json',
'Content-Type': 'application/json'}
self.log = logging.getLogger(logger)
self.set_log_level(log_level)
if syslog:
# Enables sending logging messages to the local syslog server.
self.log.addHandler(SysLogHandler())
if filename:
# Enables sending logging messages to a file.
self.log.addHandler(logging.FileHandler(filename))
if syslog is False and filename is None:
# Not logging so use the null handler
self.log.addHandler(logging.NullHandler())
# Instantiate the CvpApi class
self.api = CvpApi(self)
def Main(ctx, silent, test, allfs, scrub, filesystems, debug):
'''
perform btrfs filesystem maintenace for one, many or [-a]ll filesystems.
'''
global DEBUG, _log
DEBUG=debug
_log = logging.getLogger(LOGROOT)
_log.setLevel(logging.INFO)
sysh = SysLogHandler(address="/dev/log")
outh = logging.StreamHandler(sys.stdout)
if debug:
logfmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
sysh.setLevel(logging.DEBUG)
outh.setLevel(logging.DEBUG)
click.echo("arg allfs: %s" % str(allfs))
click.echo("arg debug: %s" % str(debug))
click.echo("arg silent: %s" % str(silent))
click.echo("arg test: %s" % str(test))
else:
logfmt = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
sysh.setLevel(logging.INFO)
outh.setLevel(logging.INFO)
sysh.setFormatter(logfmt)
outh.setFormatter(logfmt)
if len(filesystems) == 0 and not allfs:
click.echo("You must either use '-a' or specify a filesystem.")
click.echo("")
click.echo(ctx.get_help())
exit()
if test:
run = TestRun
click.echo("Run is set to Test")
_log.addHandler(sysh)
_log.addHandler(outh)
elif silent:
run = LoggerRun
_log.addHandler(sysh)
click.echo("Run is set to Logger")
else:
run = InteractiveRun
click.echo("Run is set to Interactive")
_log.addHandler(sysh)
_log.addHandler(outh)
if allfs:
filesystems = locateBtrfs()
_log.debug("arg filesystems: %s" % str(filesystems))
for fs in filesystems:
Maintain(fs, run, scrub)
continue
return True
def init_logging(config=None):
"""
Sets up the logging for the project. This will be to one of (in order)
* a log file is specified in the config
* syslog is running as a deamon
* the console otherwise
:param config: The configuration for the bot.
:type config: configparser.ConfigParser
"""
logging.basicConfig(
format='[%(asctime)s][%(levelname)-5s] %(message)s',
datefmt='%Y-%m-%d %I:%M:%S %p',
level=logging.INFO
)
if not config:
# No config (not loaded yet) basic default console logger for now
return
# See if we're to remove the console logger
if not config.getboolean('Logging', 'console', fallback=True):
for handler in logging.getLogger().handlers:
if isinstance(handler, logging.StreamHandler):
logging.getLogger().removeHandler(handler)
# Check to see if syslog logging is available
if config.getboolean('Logging', 'syslog', fallback=False):
# Defaults unless overridden
server = config.get('Syslog Logging', 'server', fallback='localhost')
port = config.getint('Syslog Logging', 'port', fallback=514)
facility = config.get('Syslog Logging', 'facility', fallback='daemon')
name = config.get('Slack', 'bot_name', fallback=__project_name__)
address = (server, port)
if '/' in server:
# An alternative to providing a (host, port) tuple is providing an address
# as a string, for example ‘/dev/log’. In this case, a Unix domain socket is
# used to send the message to the syslog.
address = server
handler = SysLogHandler(address=address, facility=facility)
# Handler does not include the process name or PID, we we have to put that in the
# message format. It's a bit ugly and I don't know why the standard syslog
# class is not used.
formatter = logging.Formatter(name + '[%(process)d]: %(message)s')
handler.setFormatter(formatter)
# Add this to the root logger. Hopefully every logger created after this will
# inherit this handler
logging.getLogger().addHandler(handler)
# Update the log level from config
level_name = config.get('Logging', 'level', fallback='info').upper()
logging.getLogger().setLevel(level_name)
def setup(logger_name: str = None,
destination: int = SYSLOG,
level: int = logging.WARNING,
propagate: bool = False) -> logging.Logger:
r"""Configures a `logging.Logger`_ object
Once configured, a logger can also be retrieved via
`logging.getLogger`_
It is inefficient to call this function more than once for the same
logger name.
Args:
logger_name:
The name of the logger to configure. Use None (the default)
to configure the root logger.
level:
The `logging level`_ to use when filtering log entries.
Defaults to logging.WARNING.
propagate:
Whether to send log entries to ancestor loggers. Defaults to False.
destination:
- SYSLOG: (the default) Send messages to syslog.
- View with: ``tail -f /var/log/syslog | sed 's/#012/\n\t/g'``
- CONSOLE: Send message to stdout
Returns:
A `logging.Logger`_ object
.. _logging.Logger: https://docs.python.org/3/library/logging.html#logging.Logger
.. _logging level: https://docs.python.org/3/library/logging.html#levels
.. _logging.getLogger: https://docs.python.org/3/library/logging.html#logging.getLogger
"""
logger = logging.getLogger(logger_name)
if destination == SYSLOG:
handler = SysLogHandler(address='/dev/log')
elif destination == CONSOLE:
handler = logging.StreamHandler(sys.stdout)
else:
raise ValueError("Invalid destination {}".format(destination))
# TODO: Consider using %r with message when using syslog .. \n looks better than #
formatter = logging.Formatter(
fmt='[FOGLAMP] %(asctime)s - %(levelname)s :: %(module)s: %(name)s: %(message)s',
datefmt='%m-%d-%Y %H:%M:%S')
handler.setFormatter(formatter)
logger.setLevel(level)
logger.propagate = propagate
logger.addHandler(handler)
return logger