Merge pull request #362 from terminalmage/logging

RFC: Improved logging
This commit is contained in:
enkore 2016-04-26 11:03:19 +02:00
commit f120fb65e3
3 changed files with 46 additions and 4 deletions

View File

@ -233,7 +233,43 @@ to files in your home directory named
``.i3pystatus-<pid-of-thread>``. Some modules might log additional ``.i3pystatus-<pid-of-thread>``. Some modules might log additional
information. information.
.. rubric:: Log level Setting a specific logfile
~~~~~~~~~~~~~~~~~~~~~~~~~~
When instantiating your ``Status`` object, the path to a log file can be
specified. If this is done, then log messages will be sent to that file and not
to an ``.i3pystatus-<pid-of-thread>`` file in your home directory. This is
useful in that it helps keep your home directory from becoming cluttered with
files containing errors.
.. code-block:: python
from i3pystatus import Status
status = Status(logfile='/home/username/var/i3pystatus.log')
Changing log format
~~~~~~~~~~~~~~~~~~~
.. versionadded:: 3.35
The ``logformat`` option can be useed to change the format of the log files,
using `LogRecord attributes`__.
.. code-block:: python
from i3pystatus import Status
status = Status(
logfile='/home/username/var/i3pystatus.log',
logformat='%(asctime)s %(levelname)s:',
)
.. __: https://docs.python.org/3/library/logging.html#logrecord-attributes
Log level
~~~~~~~~~
Every module has a ``log_level`` option which sets the *minimum* Every module has a ``log_level`` option which sets the *minimum*
severity required for an event to be logged. severity required for an event to be logged.

View File

@ -8,6 +8,8 @@ from i3pystatus.core.exceptions import ConfigError
from i3pystatus.core.imputil import ClassFinder from i3pystatus.core.imputil import ClassFinder
from i3pystatus.core.modules import Module from i3pystatus.core.modules import Module
DEFAULT_LOG_FORMAT = '%(asctime)s [%(levelname)-8s][%(name)s %(lineno)d] %(message)s'
class CommandEndpoint: class CommandEndpoint:
""" """
@ -59,17 +61,21 @@ class Status:
""" """
def __init__(self, standalone=True, click_events=True, interval=1, def __init__(self, standalone=True, click_events=True, interval=1,
input_stream=None, logfile=None, internet_check=None): input_stream=None, logfile=None, internet_check=None,
logformat=DEFAULT_LOG_FORMAT):
self.standalone = standalone self.standalone = standalone
self.click_events = standalone and click_events self.click_events = standalone and click_events
input_stream = input_stream or sys.stdin input_stream = input_stream or sys.stdin
if logfile:
logger = logging.getLogger("i3pystatus") logger = logging.getLogger("i3pystatus")
if logfile:
for handler in logger.handlers: for handler in logger.handlers:
logger.removeHandler(handler) logger.removeHandler(handler)
handler = logging.FileHandler(logfile, delay=True) handler = logging.FileHandler(logfile, delay=True)
logger.addHandler(handler) logger.addHandler(handler)
logger.setLevel(logging.CRITICAL) logger.setLevel(logging.CRITICAL)
if logformat:
for index in range(len(logger.handlers)):
logger.handlers[index].setFormatter(logging.Formatter(logformat))
if internet_check: if internet_check:
util.internet.address = internet_check util.internet.address = internet_check

View File

@ -67,7 +67,7 @@ class ExceptionWrapper(Wrapper):
try: try:
self.workload() self.workload()
except: except:
message = "\n> Exception in {thread} at {time}, module {name}".format( message = "Exception in {thread} at {time}, module {name}".format(
thread=threading.current_thread().name, thread=threading.current_thread().name,
time=time.strftime("%c"), time=time.strftime("%c"),
name=self.workload.__class__.__name__ name=self.workload.__class__.__name__