Skip to content

Commit

Permalink
Use the logging module directly instead of home-grown code.
Browse files Browse the repository at this point in the history
This has the advantage of overall less custom code; as well as support
for per-module configuration. This would enable a potential solution for
ultrabug#1479, since in the future
it can allow per-module configuration of log levels.

I expect this to mainly help module creators, allowing them to enable
logging for only their module, while disabling all other messages. It
also is easier to use, since the `logging` module's interface is
generally simpler than `self.py3`.

Here, I've made the decision to keep the message format as close as
possible to the existing log messages.
  • Loading branch information
rlerm authored and lasers committed Jan 21, 2024
1 parent 8bf3e58 commit 5efa975
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion py3status/core.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import logging
import logging.handlers
import logging.config
import sys
import time
from collections import deque
Expand Down Expand Up @@ -32,6 +31,12 @@
}


LOGGING_LEVELS = {
"error": logging.ERROR,
"warning": logging.WARNING,
"info": logging.INFO,
}

DBUS_LEVELS = {"error": "critical", "warning": "normal", "info": "low"}

CONFIG_SPECIAL_SECTIONS = [
Expand Down Expand Up @@ -531,9 +536,35 @@ def load_modules(self, modules_list, user_modules):
msg = f'Loading module "{module}" failed ({err}).'
self.report_exception(msg, level="warning")

def _setup_logging(self):
"""Set up the global logger."""
root = logging.getLogger(name=None)
if self.config.get("debug"):
root.setLevel(logging.DEBUG)
else:
root.setLevel(logging.DEBUG)

log_file = self.config.get("log_file")
if log_file:
handler = logging.FileHandler(log_file, encoding='utf8')
else:
logging.handlers.SysLogHandler()
handler.setFormatter(logging.Formatter(
fmt='%(asctime)s %(levelname)s %(module)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
style='%'))
root.addHandler(handler)

def setup(self):
"""
Setup py3status and spawn i3status/events/modules threads.
"""
self._setup_logging()

def _log_gitversion(self):
# A git repo is detected looking for the .git directory


git_path = Path(__file__).resolve().parent.parent / ".git"
if not git_path.exists():
return
Expand Down

0 comments on commit 5efa975

Please sign in to comment.