This repository has been archived on 2022-01-23. You can view files and clone it, but cannot push or open issues or pull requests.
vaultpass/vaultpass/logger.py

58 lines
2.0 KiB
Python

import logging
import logging.handlers
import os
##
from . import constants
##
try:
# https://www.freedesktop.org/software/systemd/python-systemd/journal.html#journalhandler-class
from systemd import journal
_has_journald = True
except ImportError:
_has_journald = False
def prepLogfile(path = constants.DEFAULT_LOGFILE):
path = os.path.abspath(os.path.expanduser(path))
# Set up the permissions beforehand.
os.makedirs(os.path.dirname(path), exist_ok = True, mode = 0o0700)
if not os.path.isfile(path):
# "Touch" it so the next command doesn't fail.
with open(path, 'w') as fh:
fh.write('')
os.chmod(path, 0o0600)
return(path)
_cfg_args = {'handlers': [],
'level': constants.DEFAULT_LOGLEVEL}
if _has_journald:
# There were some weird changes somewhere along the line.
try:
# But it's *probably* this one.
h = journal.JournalHandler()
except AttributeError:
h = journal.JournaldLogHandler()
# Systemd includes times, so we don't need to.
h.setFormatter(logging.Formatter(style = '{',
fmt = ('{name}:{levelname}:{name}:{filename}:'
'{funcName}:{lineno}: {message}')))
_cfg_args['handlers'].append(h)
# Logfile
h = logging.handlers.RotatingFileHandler(prepLogfile(),
encoding = 'utf8',
# Disable rotating for now.
# maxBytes = 50000000000,
# backupCount = 30
)
h.setFormatter(logging.Formatter(style = '{',
fmt = ('{asctime}:'
'{levelname}:{name}:{filename}:'
'{funcName}:{lineno}: {message}')))
_cfg_args['handlers'].append(h)
logging.basicConfig(**_cfg_args)
logger = logging.getLogger('VaultPass')
logger.info('Logging initialized.')