2011-01-18 09:24:57 +00:00
|
|
|
#
|
|
|
|
# anaconda_log.py: Support for logging to multiple destinations with log
|
|
|
|
# levels.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000, 2001, 2002, 2005 Red Hat, Inc. All rights reserved.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# Author(s): Chris Lumens <clumens@redhat.com>
|
|
|
|
# Matt Wilson <msw@redhat.com>
|
|
|
|
# Michael Fulbright <msf@redhat.com>
|
|
|
|
#
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
import logging
|
2015-05-30 11:20:59 +00:00
|
|
|
from logging.handlers import SysLogHandler, SocketHandler, SYSLOG_UDP_PORT
|
2011-01-18 09:24:57 +00:00
|
|
|
import os
|
|
|
|
import sys
|
2013-01-23 17:28:19 +00:00
|
|
|
import warnings
|
2011-01-18 09:24:57 +00:00
|
|
|
|
2014-04-07 12:38:09 +00:00
|
|
|
from pyanaconda.flags import flags
|
2015-03-23 11:36:12 +00:00
|
|
|
from pyanaconda.constants import LOGLVL_LOCK
|
2011-01-18 09:24:57 +00:00
|
|
|
|
2015-05-30 11:20:59 +00:00
|
|
|
DEFAULT_LEVEL = logging.INFO
|
2011-01-18 09:24:57 +00:00
|
|
|
ENTRY_FORMAT = "%(asctime)s,%(msecs)03d %(levelname)s %(name)s: %(message)s"
|
|
|
|
STDOUT_FORMAT = "%(asctime)s %(message)s"
|
|
|
|
DATE_FORMAT = "%H:%M:%S"
|
|
|
|
|
|
|
|
MAIN_LOG_FILE = "/tmp/anaconda.log"
|
|
|
|
PROGRAM_LOG_FILE = "/tmp/program.log"
|
2013-01-23 17:28:19 +00:00
|
|
|
STORAGE_LOG_FILE = "/tmp/storage.log"
|
|
|
|
PACKAGING_LOG_FILE = "/tmp/packaging.log"
|
2014-04-07 12:38:09 +00:00
|
|
|
SENSITIVE_INFO_LOG_FILE = "/tmp/sensitive-info.log"
|
2011-01-18 09:24:57 +00:00
|
|
|
ANACONDA_SYSLOG_FACILITY = SysLogHandler.LOG_LOCAL1
|
|
|
|
|
2014-04-07 12:38:09 +00:00
|
|
|
from threading import Lock
|
|
|
|
program_log_lock = Lock()
|
|
|
|
|
2015-03-23 11:36:12 +00:00
|
|
|
logLevelMap = {"lock": LOGLVL_LOCK,
|
|
|
|
"debug": logging.DEBUG,
|
|
|
|
"info": logging.INFO,
|
|
|
|
"warning": logging.WARNING,
|
|
|
|
"error": logging.ERROR,
|
2011-01-18 09:24:57 +00:00
|
|
|
"critical": logging.CRITICAL}
|
|
|
|
|
|
|
|
# sets autoSetLevel for the given handler
|
|
|
|
def autoSetLevel(handler, value):
|
|
|
|
handler.autoSetLevel = value
|
|
|
|
|
|
|
|
# all handlers of given logger with autoSetLevel == True are set to level
|
2014-04-07 12:38:09 +00:00
|
|
|
def setHandlersLevel(logr, level):
|
2016-04-10 04:00:00 +00:00
|
|
|
for handler in filter(lambda hdlr: hasattr(hdlr, "autoSetLevel") and hdlr.autoSetLevel, logr.handlers):
|
|
|
|
handler.setLevel(level)
|
2013-01-23 17:28:19 +00:00
|
|
|
|
2011-01-18 09:24:57 +00:00
|
|
|
class AnacondaSyslogHandler(SysLogHandler):
|
2015-03-23 11:36:12 +00:00
|
|
|
# syslog doesn't understand these level names
|
2016-04-10 04:00:00 +00:00
|
|
|
levelMap = {"ERR": "error",
|
|
|
|
"CRIT": "critical",
|
|
|
|
"LOCK": "debug"}
|
2015-03-23 11:36:12 +00:00
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
def __init__(self,
|
2011-01-18 09:24:57 +00:00
|
|
|
address=('localhost', SYSLOG_UDP_PORT),
|
2013-01-23 17:28:19 +00:00
|
|
|
facility=SysLogHandler.LOG_USER,
|
2011-01-18 09:24:57 +00:00
|
|
|
tag=''):
|
|
|
|
self.tag = tag
|
|
|
|
SysLogHandler.__init__(self, address, facility)
|
|
|
|
|
|
|
|
def emit(self, record):
|
|
|
|
original_msg = record.msg
|
|
|
|
record.msg = '%s: %s' %(self.tag, original_msg)
|
|
|
|
SysLogHandler.emit(self, record)
|
|
|
|
record.msg = original_msg
|
|
|
|
|
2015-03-23 11:36:12 +00:00
|
|
|
def mapPriority(self, level):
|
|
|
|
"""Map the priority level to a syslog level """
|
|
|
|
return self.levelMap.get(level, SysLogHandler.mapPriority(self, level))
|
|
|
|
|
2015-05-30 11:20:59 +00:00
|
|
|
class AnacondaSocketHandler(SocketHandler):
|
|
|
|
def makePickle(self, record):
|
2016-04-10 04:00:00 +00:00
|
|
|
return bytes(self.formatter.format(record) + "\n", "utf-8")
|
2015-05-30 11:20:59 +00:00
|
|
|
|
2011-01-18 09:24:57 +00:00
|
|
|
class AnacondaLog:
|
2016-04-10 04:00:00 +00:00
|
|
|
SYSLOG_CFGFILE = "/etc/rsyslog.conf"
|
2013-01-23 17:28:19 +00:00
|
|
|
VIRTIO_PORT = "/dev/virtio-ports/org.fedoraproject.anaconda.log.0"
|
|
|
|
|
2016-04-10 04:00:00 +00:00
|
|
|
def __init__(self):
|
2015-05-30 11:20:59 +00:00
|
|
|
self.loglevel = DEFAULT_LEVEL
|
2011-01-18 09:24:57 +00:00
|
|
|
self.remote_syslog = None
|
2013-01-23 17:28:19 +00:00
|
|
|
# Rename the loglevels so they are the same as in syslog.
|
|
|
|
logging.addLevelName(logging.WARNING, "WARN")
|
|
|
|
logging.addLevelName(logging.ERROR, "ERR")
|
|
|
|
logging.addLevelName(logging.CRITICAL, "CRIT")
|
2015-03-23 11:36:12 +00:00
|
|
|
logging.addLevelName(LOGLVL_LOCK, "LOCK")
|
2013-01-23 17:28:19 +00:00
|
|
|
|
|
|
|
# Create the base of the logger hierarchy.
|
|
|
|
self.anaconda_logger = logging.getLogger("anaconda")
|
|
|
|
self.addFileHandler(MAIN_LOG_FILE, self.anaconda_logger,
|
|
|
|
minLevel=logging.DEBUG)
|
|
|
|
warnings.showwarning = self.showwarning
|
|
|
|
|
|
|
|
# Create the storage logger.
|
2014-04-07 12:38:09 +00:00
|
|
|
storage_logger = logging.getLogger("blivet")
|
2013-01-23 17:28:19 +00:00
|
|
|
self.addFileHandler(STORAGE_LOG_FILE, storage_logger,
|
2011-01-18 09:24:57 +00:00
|
|
|
minLevel=logging.DEBUG)
|
2013-01-23 17:28:19 +00:00
|
|
|
|
|
|
|
# Set the common parameters for anaconda and storage loggers.
|
2014-04-07 12:38:09 +00:00
|
|
|
for logr in [self.anaconda_logger, storage_logger]:
|
|
|
|
logr.setLevel(logging.DEBUG)
|
|
|
|
self.forwardToSyslog(logr)
|
2011-01-18 09:24:57 +00:00
|
|
|
|
|
|
|
# External program output log
|
|
|
|
program_logger = logging.getLogger("program")
|
|
|
|
program_logger.setLevel(logging.DEBUG)
|
|
|
|
self.addFileHandler(PROGRAM_LOG_FILE, program_logger,
|
|
|
|
minLevel=logging.DEBUG)
|
|
|
|
self.forwardToSyslog(program_logger)
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
# Create the packaging logger.
|
|
|
|
packaging_logger = logging.getLogger("packaging")
|
2015-03-23 11:36:12 +00:00
|
|
|
packaging_logger.setLevel(LOGLVL_LOCK)
|
2013-01-23 17:28:19 +00:00
|
|
|
self.addFileHandler(PACKAGING_LOG_FILE, packaging_logger,
|
2014-04-07 12:38:09 +00:00
|
|
|
minLevel=logging.INFO,
|
|
|
|
autoLevel=True)
|
2013-01-23 17:28:19 +00:00
|
|
|
self.forwardToSyslog(packaging_logger)
|
|
|
|
|
|
|
|
# Create the yum logger and link it to packaging
|
|
|
|
yum_logger = logging.getLogger("yum")
|
|
|
|
yum_logger.setLevel(logging.DEBUG)
|
|
|
|
self.addFileHandler(PACKAGING_LOG_FILE, yum_logger,
|
|
|
|
minLevel=logging.DEBUG)
|
|
|
|
self.forwardToSyslog(yum_logger)
|
|
|
|
|
2014-04-07 12:38:09 +00:00
|
|
|
# Create the sensitive information logger
|
|
|
|
# * the sensitive-info.log file is not copied to the installed
|
|
|
|
# system, as it might contain sensitive information that
|
|
|
|
# should not be persistently stored by default
|
|
|
|
sensitive_logger = logging.getLogger("sensitive-info")
|
|
|
|
self.addFileHandler(SENSITIVE_INFO_LOG_FILE, sensitive_logger,
|
|
|
|
minLevel=logging.DEBUG)
|
|
|
|
|
2011-01-18 09:24:57 +00:00
|
|
|
# Create a second logger for just the stuff we want to dup on
|
|
|
|
# stdout. Anything written here will also get passed up to the
|
|
|
|
# parent loggers for processing and possibly be written to the
|
|
|
|
# log.
|
|
|
|
stdoutLogger = logging.getLogger("anaconda.stdout")
|
|
|
|
stdoutLogger.setLevel(logging.INFO)
|
|
|
|
# Add a handler for the duped stuff. No fancy formatting, thanks.
|
|
|
|
self.addFileHandler(sys.stdout, stdoutLogger,
|
|
|
|
fmtStr=STDOUT_FORMAT, minLevel=logging.INFO)
|
|
|
|
|
|
|
|
# Stderr logger
|
|
|
|
stderrLogger = logging.getLogger("anaconda.stderr")
|
|
|
|
stderrLogger.setLevel(logging.INFO)
|
|
|
|
self.addFileHandler(sys.stderr, stderrLogger,
|
|
|
|
fmtStr=STDOUT_FORMAT, minLevel=logging.INFO)
|
|
|
|
|
|
|
|
# Add a simple handler - file or stream, depending on what we're given.
|
2016-04-10 04:00:00 +00:00
|
|
|
def addFileHandler(self, dest, addToLogger, minLevel=DEFAULT_LEVEL,
|
2011-01-18 09:24:57 +00:00
|
|
|
fmtStr=ENTRY_FORMAT,
|
|
|
|
autoLevel=False):
|
2014-04-07 12:38:09 +00:00
|
|
|
try:
|
2016-04-10 04:00:00 +00:00
|
|
|
if isinstance(dest, str):
|
2014-04-07 12:38:09 +00:00
|
|
|
logfileHandler = logging.FileHandler(dest)
|
|
|
|
else:
|
|
|
|
logfileHandler = logging.StreamHandler(dest)
|
|
|
|
|
|
|
|
logfileHandler.setLevel(minLevel)
|
|
|
|
logfileHandler.setFormatter(logging.Formatter(fmtStr, DATE_FORMAT))
|
|
|
|
autoSetLevel(logfileHandler, autoLevel)
|
|
|
|
addToLogger.addHandler(logfileHandler)
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def forwardToSyslog(self, logr):
|
2011-01-18 09:24:57 +00:00
|
|
|
"""Forward everything that goes in the logger to the syslog daemon.
|
|
|
|
"""
|
2015-03-23 11:36:12 +00:00
|
|
|
if flags.imageInstall or flags.dirInstall:
|
2013-01-23 17:28:19 +00:00
|
|
|
# don't clutter up the system logs when doing an image install
|
|
|
|
return
|
|
|
|
|
2011-01-18 09:24:57 +00:00
|
|
|
syslogHandler = AnacondaSyslogHandler(
|
2013-01-23 17:28:19 +00:00
|
|
|
'/dev/log',
|
2011-01-18 09:24:57 +00:00
|
|
|
ANACONDA_SYSLOG_FACILITY,
|
2014-04-07 12:38:09 +00:00
|
|
|
logr.name)
|
2011-01-18 09:24:57 +00:00
|
|
|
syslogHandler.setLevel(logging.DEBUG)
|
2014-04-07 12:38:09 +00:00
|
|
|
logr.addHandler(syslogHandler)
|
2011-01-18 09:24:57 +00:00
|
|
|
|
2015-03-23 11:36:12 +00:00
|
|
|
# pylint: disable=redefined-builtin
|
2013-01-23 17:28:19 +00:00
|
|
|
def showwarning(self, message, category, filename, lineno,
|
|
|
|
file=sys.stderr, line=None):
|
|
|
|
""" Make sure messages sent through python's warnings module get logged.
|
|
|
|
|
|
|
|
The warnings mechanism is used by some libraries we use,
|
|
|
|
notably pykickstart.
|
|
|
|
"""
|
2015-03-23 11:36:12 +00:00
|
|
|
self.anaconda_logger.warning("%s", warnings.formatwarning(
|
2013-01-23 17:28:19 +00:00
|
|
|
message, category, filename, lineno, line))
|
|
|
|
|
2015-05-30 11:20:59 +00:00
|
|
|
def setup_remotelog(self, host, port):
|
|
|
|
remotelog = AnacondaSocketHandler(host, port)
|
|
|
|
remotelog.setFormatter(logging.Formatter(ENTRY_FORMAT, DATE_FORMAT))
|
|
|
|
remotelog.setLevel(logging.DEBUG)
|
|
|
|
logging.getLogger().addHandler(remotelog)
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
def restartSyslog(self):
|
2015-05-30 11:20:59 +00:00
|
|
|
# Import here instead of at the module level to avoid an import loop
|
|
|
|
from pyanaconda.iutil import execWithRedirect
|
|
|
|
execWithRedirect("systemctl", ["restart", "rsyslog.service"])
|
2013-01-23 17:28:19 +00:00
|
|
|
|
2011-01-18 09:24:57 +00:00
|
|
|
def updateRemote(self, remote_syslog):
|
|
|
|
"""Updates the location of remote rsyslogd to forward to.
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
Requires updating rsyslogd config and restarting rsyslog
|
2011-01-18 09:24:57 +00:00
|
|
|
"""
|
2016-04-10 04:00:00 +00:00
|
|
|
# Import here instead of at the module level to avoid an import loop
|
|
|
|
from pyanaconda.iutil import open # pylint: disable=redefined-builtin
|
|
|
|
|
2011-01-18 09:24:57 +00:00
|
|
|
TEMPLATE = "*.* @@%s\n"
|
|
|
|
|
|
|
|
self.remote_syslog = remote_syslog
|
2013-01-23 17:28:19 +00:00
|
|
|
with open(self.SYSLOG_CFGFILE, 'a') as cfgfile:
|
2011-01-18 09:24:57 +00:00
|
|
|
forward_line = TEMPLATE % remote_syslog
|
|
|
|
cfgfile.write(forward_line)
|
2013-01-23 17:28:19 +00:00
|
|
|
self.restartSyslog()
|
|
|
|
|
|
|
|
def setupVirtio(self):
|
|
|
|
"""Setup virtio rsyslog logging.
|
|
|
|
"""
|
2016-04-10 04:00:00 +00:00
|
|
|
# Import here instead of at the module level to avoid an import loop
|
|
|
|
from pyanaconda.iutil import open # pylint: disable=redefined-builtin
|
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
TEMPLATE = "*.* %s;anaconda_syslog\n"
|
|
|
|
|
2016-04-10 04:00:00 +00:00
|
|
|
vport = flags.cmdline.get('virtiolog') or self.VIRTIO_PORT
|
2015-05-30 11:20:59 +00:00
|
|
|
|
|
|
|
if not os.access(vport, os.W_OK):
|
2013-01-23 17:28:19 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
with open(self.SYSLOG_CFGFILE, 'a') as cfgfile:
|
2015-05-30 11:20:59 +00:00
|
|
|
cfgfile.write(TEMPLATE % (vport,))
|
2013-01-23 17:28:19 +00:00
|
|
|
self.restartSyslog()
|
|
|
|
|
2011-01-18 09:24:57 +00:00
|
|
|
|
2013-01-23 17:28:19 +00:00
|
|
|
logger = None
|
|
|
|
def init():
|
|
|
|
global logger
|
|
|
|
logger = AnacondaLog()
|