Configurable log level: ENV + cmd
This commit is contained in:
parent
4ad575cfe3
commit
947a56b21e
|
@ -4,6 +4,8 @@ services:
|
||||||
build: .
|
build: .
|
||||||
env_file:
|
env_file:
|
||||||
- restic_compose_backup.env
|
- restic_compose_backup.env
|
||||||
|
# environment:
|
||||||
|
# - LOG_LEVEL=error
|
||||||
volumes:
|
volumes:
|
||||||
# Map in docker socket
|
# Map in docker socket
|
||||||
- /var/run/docker.sock:/tmp/docker.sock:ro
|
- /var/run/docker.sock:/tmp/docker.sock:ro
|
||||||
|
|
|
@ -16,6 +16,7 @@ logger = logging.getLogger(__name__)
|
||||||
def main():
|
def main():
|
||||||
"""CLI entrypoint"""
|
"""CLI entrypoint"""
|
||||||
args = parse_args()
|
args = parse_args()
|
||||||
|
log.setup(level=args.log_level)
|
||||||
config = Config()
|
config = Config()
|
||||||
containers = RunningContainers()
|
containers = RunningContainers()
|
||||||
|
|
||||||
|
@ -132,6 +133,12 @@ def parse_args():
|
||||||
'action',
|
'action',
|
||||||
choices=['status', 'backup', 'start-backup-process'],
|
choices=['status', 'backup', 'start-backup-process'],
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--log-level',
|
||||||
|
default=None,
|
||||||
|
choices=list(log.LOG_LEVELS.keys()),
|
||||||
|
help="Log level"
|
||||||
|
)
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,25 @@ import sys
|
||||||
logger = logging.getLogger('restic_compose_backup')
|
logger = logging.getLogger('restic_compose_backup')
|
||||||
HOSTNAME = os.environ['HOSTNAME']
|
HOSTNAME = os.environ['HOSTNAME']
|
||||||
|
|
||||||
level = logging.INFO
|
DEFAULT_LOG_LEVEL = logging.INFO
|
||||||
logger.setLevel(level)
|
LOG_LEVELS = {
|
||||||
ch = logging.StreamHandler(stream=sys.stdout)
|
'debug': logging.DEBUG,
|
||||||
ch.setLevel(level)
|
'info': logging.INFO,
|
||||||
ch.setFormatter(logging.Formatter(f'%(asctime)s - {HOSTNAME} - %(name)s - %(levelname)s - %(message)s'))
|
'warning': logging.WARNING,
|
||||||
logger.addHandler(ch)
|
'error': logging.ERROR,
|
||||||
|
}
|
||||||
|
|
||||||
|
def setup(level: str = 'warning'):
|
||||||
|
"""Set up logging"""
|
||||||
|
# Get log level from env if not set directly
|
||||||
|
if level is None:
|
||||||
|
level = os.environ.get('LOG_LEVEL')
|
||||||
|
|
||||||
|
level = level or ""
|
||||||
|
level = LOG_LEVELS.get(level.lower(), DEFAULT_LOG_LEVEL)
|
||||||
|
logger.setLevel(level)
|
||||||
|
|
||||||
|
ch = logging.StreamHandler(stream=sys.stdout)
|
||||||
|
ch.setLevel(level)
|
||||||
|
ch.setFormatter(logging.Formatter(f'%(asctime)s - {HOSTNAME} - %(name)s - %(levelname)s - %(message)s'))
|
||||||
|
logger.addHandler(ch)
|
||||||
|
|
Loading…
Reference in New Issue