Log to journal if JOURNAL_STREAM env var is set
Replace 'journal' with 'console' Always log to console when nit logging to journal in addition to any file logging
This commit is contained in:
+1
-1
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
||||
|
||||
[project]
|
||||
name = "hf-cli"
|
||||
version = "1.2.1"
|
||||
version = "1.2.2"
|
||||
description = "Huafetcher CLI"
|
||||
readme = "README.md"
|
||||
license = {file = "LICENSE"}
|
||||
|
||||
@@ -3,8 +3,11 @@
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import typing
|
||||
import re
|
||||
from logging import StreamHandler
|
||||
|
||||
from xdg import BaseDirectory as Xdg
|
||||
from argparse import Namespace
|
||||
from getpass import getpass
|
||||
@@ -105,13 +108,12 @@ class ConfigHandler:
|
||||
if hasattr(args, 'verbose') and args.verbose is not None:
|
||||
self.config.verbose = args.verbose
|
||||
if args.setup:
|
||||
self.config.log_to = ConfigHandler.request_text_input_with_fallback("Where should logs be written? 'journal' logs to the systemd journal, 'disabled' disables logging, empty uses the default '~/.local/state/hf_cli'", getattr(self.config, 'log_to', ''))
|
||||
self.config.log_to = ConfigHandler.request_text_input_with_fallback("Where should logs be written, when not running as a systemd service? A path or 'disabled', which disables logging or '' to use the default location '~/.local/state/hf_cli'", getattr(self.config, 'log_to', ''))
|
||||
self.config.verbose = ConfigHandler.request_boolean_with_fallback('Would you like to always log verbosely?', getattr(self.config, 'verbose', False))
|
||||
if hasattr(self.config, 'log_to') and self.config.log_to is not None and self.config.log_to != 'disabled':
|
||||
if self.config.log_to == 'journal':
|
||||
if 'JOURNAL_STREAM' in os.environ:
|
||||
from systemd.journal import JournalHandler
|
||||
self.log.addHandler(JournalHandler())
|
||||
config_change = True
|
||||
else:
|
||||
if self.config.log_to and not os.path.isabs(self.config.log_to) and (os.path.exists(self.config.log_to) or os.path.exists(os.path.dirname(self.config.log_to))):
|
||||
self.config.log_to = os.path.abspath(os.path.expanduser(self.config.log_to))
|
||||
@@ -123,11 +125,12 @@ class ConfigHandler:
|
||||
os.makedirs(os.path.dirname(self.config.log_to), exist_ok=True)
|
||||
self.log.addHandler(FileHandler(self.config.log_to))
|
||||
config_change = True
|
||||
else:
|
||||
elif self.config.log_to != 'console':
|
||||
from logging import FileHandler
|
||||
self.config.log_to = Xdg.save_state_path('hf-cli') + '/hf-cli.log'
|
||||
self.log.addHandler(FileHandler(self.config.log_to))
|
||||
config_change = True
|
||||
self.log.addHandler(StreamHandler(sys.stdout))
|
||||
self.log.setLevel(logging.DEBUG if getattr(self.config, 'verbose', False) else logging.INFO)
|
||||
else:
|
||||
self.log.propagate = False
|
||||
|
||||
@@ -15,7 +15,7 @@ class Part(Enum):
|
||||
UIHH = 3
|
||||
LOG_ABSOLUTE = 4
|
||||
LOG_RELATIVE = 5
|
||||
LOG_JOURNAL = 6
|
||||
LOG_CONSOLE = 6
|
||||
LOG_LOCAL = 7
|
||||
LOG_DISABLED = 8
|
||||
VERBOSE = 9
|
||||
@@ -46,8 +46,8 @@ class ConfigGenerator:
|
||||
config['log_to'] = 'disabled'
|
||||
case Part.LOG_ABSOLUTE:
|
||||
config['log_to'] = '/tmp'
|
||||
case Part.LOG_JOURNAL:
|
||||
config['log_to'] = 'journal'
|
||||
case Part.LOG_CONSOLE:
|
||||
config['log_to'] = 'console'
|
||||
case Part.LOG_RELATIVE:
|
||||
config['log_to'] = os.path.abspath('./test.log')
|
||||
case Part.VERBOSE:
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import time
|
||||
import os
|
||||
from logging import FileHandler
|
||||
import unittest
|
||||
import inspect
|
||||
|
||||
from logging import FileHandler, StreamHandler
|
||||
from systemd.journal import JournalHandler
|
||||
|
||||
from hf_cli.args_handler import ArgsHandler
|
||||
@@ -19,8 +18,8 @@ class ConfigHandlerTest(unittest.TestCase):
|
||||
def test_default_config_path(self):
|
||||
args = ArgsHandler().parse_args(['EPO'])
|
||||
actual = ConfigHandler.get_config_path(args)
|
||||
expect = '/etc/hf-cli/config.json' if os.getuid() == 0 else os.path.expanduser('~/.config/hf-cli/config.json')
|
||||
self.assertEqual(expect, actual)
|
||||
expected = '/etc/hf-cli/config.json' if os.getuid() == 0 else os.path.expanduser('~/.config/hf-cli/config.json')
|
||||
self.assertEqual(expected, actual)
|
||||
|
||||
def test_provided_config_path(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS) as config_dir_path:
|
||||
@@ -64,7 +63,7 @@ class ConfigHandlerTest(unittest.TestCase):
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path)])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertFalse(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertTrue(sut.log.disabled, get_failure_message(args, sut))
|
||||
|
||||
@@ -73,70 +72,70 @@ class ConfigHandlerTest(unittest.TestCase):
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path)])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertFalse(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertTrue(sut.log.disabled, get_failure_message(args, sut))
|
||||
|
||||
def test_provided_disable_logging_value(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_JOURNAL) as config_dir_path:
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_CONSOLE) as config_dir_path:
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path), '-l', 'disabled'])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertFalse(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertTrue(sut.log.disabled, get_failure_message(args, sut))
|
||||
|
||||
def test_provided_disable_logging_short(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_JOURNAL) as config_dir_path:
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_CONSOLE) as config_dir_path:
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path), '-q'])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertFalse(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertTrue(sut.log.disabled, get_failure_message(args, sut))
|
||||
|
||||
def test_provided_disable_logging_short_negation(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_JOURNAL) as config_dir_path:
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_CONSOLE) as config_dir_path:
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path), '+l'])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertFalse(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertTrue(sut.log.disabled, get_failure_message(args, sut))
|
||||
|
||||
def test_provided_logging(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS) as config_dir_path:
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path), '-l', 'journal'])
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path), '-l', 'console'])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertTrue(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertEqual(JournalHandler, sut.log.handlers[0].__class__, get_failure_message(args, sut))
|
||||
self.assertEqual(StreamHandler, sut.log.handlers[0].__class__, get_failure_message(args, sut))
|
||||
|
||||
def test_replace_logging(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_ABSOLUTE) as config_dir_path:
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path), '-l', 'journal'])
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path), '-l', 'console'])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertTrue(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertEqual(JournalHandler, sut.log.handlers[0].__class__, get_failure_message(args, sut))
|
||||
self.assertEqual(StreamHandler, sut.log.handlers[0].__class__, get_failure_message(args, sut))
|
||||
|
||||
def test_configured_journal_logging(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_JOURNAL) as config_dir_path:
|
||||
def test_configured_only_console_logging(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_CONSOLE) as config_dir_path:
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path)])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertTrue(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertEqual(JournalHandler, sut.log.handlers[0].__class__, get_failure_message(args, sut))
|
||||
self.assertEqual(StreamHandler, sut.log.handlers[0].__class__, get_failure_message(args, sut))
|
||||
|
||||
def test_configured_absolute_logging(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_ABSOLUTE) as config_dir_path:
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path)])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertTrue(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertEqual(FileHandler, sut.log.handlers[0].__class__, get_failure_message(args, sut))
|
||||
self.assertEqual('/tmp/hf-cli.log', sut.log.handlers[0].baseFilename, get_failure_message(args, sut))
|
||||
@@ -146,7 +145,7 @@ class ConfigHandlerTest(unittest.TestCase):
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path)])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertTrue(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertEqual(FileHandler, sut.log.handlers[0].__class__, get_failure_message(args, sut))
|
||||
self.assertEqual(os.path.abspath('./test.log'), sut.log.handlers[0].baseFilename, get_failure_message(args, sut))
|
||||
@@ -157,7 +156,7 @@ class ConfigHandlerTest(unittest.TestCase):
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path)])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.log.name, name)
|
||||
self.assertEqual(name, sut.log.name)
|
||||
self.assertTrue(sut.log.hasHandlers(), get_failure_message(args, sut))
|
||||
self.assertEqual(FileHandler, sut.log.handlers[0].__class__, get_failure_message(args, sut))
|
||||
self.assertEqual(os.path.expanduser('~/.local/state/hf-cli/hf-cli.log'), sut.log.handlers[0].baseFilename, get_failure_message(args, sut))
|
||||
@@ -167,21 +166,21 @@ class ConfigHandlerTest(unittest.TestCase):
|
||||
args = ArgsHandler().parse_args(['-C', '-c', self.__config_file_generator.get_config_file_path(config_dir_path)])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.config.targets, [ 'EPO', 'AGPS' ], get_failure_message(args, sut))
|
||||
self.assertEqual([ 'EPO', 'AGPS' ], sut.config.targets, get_failure_message(args, sut))
|
||||
|
||||
def test_provided_targets(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS) as config_dir_path:
|
||||
args = ArgsHandler().parse_args(['-c', self.__config_file_generator.get_config_file_path(config_dir_path), 'AGPSZIP', 'LLE'])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.config.targets, [ 'AGPSZIP', 'LLE' ], get_failure_message(args, sut))
|
||||
self.assertEqual([ 'AGPSZIP', 'LLE' ], sut.config.targets, get_failure_message(args, sut))
|
||||
|
||||
def test_provided_overrides_provided_targets(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS) as config_dir_path:
|
||||
args = ArgsHandler().parse_args(['-c', self.__config_file_generator.get_config_file_path(config_dir_path), 'AGPSZIP', 'LLE'])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
self.assertEqual(sut.config.targets, [ 'AGPSZIP', 'LLE' ], get_failure_message(args, sut))
|
||||
self.assertEqual([ 'AGPSZIP', 'LLE' ], sut.config.targets, get_failure_message(args, sut))
|
||||
|
||||
def test_configure_huamidevice_configured_targets(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS) as config_dir_path:
|
||||
@@ -242,7 +241,7 @@ class ConfigHandlerTest(unittest.TestCase):
|
||||
self.assertFalse(sut.config.uihh, get_failure_message(args, sut))
|
||||
|
||||
def test_save(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.TARGET_DIR, Part.UIHH, Part.LOG_JOURNAL) as config_dir_path:
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.TARGET_DIR, Part.UIHH, Part.LOG_CONSOLE) as config_dir_path:
|
||||
config_file_path = self.__config_file_generator.get_config_file_path(config_dir_path)
|
||||
original_timestamp = os.path.getmtime(config_file_path)
|
||||
config_before = ConfigHandler.load_config_json(config_file_path)
|
||||
@@ -259,10 +258,10 @@ class ConfigHandlerTest(unittest.TestCase):
|
||||
self.assertNotEqual(config_before.target_dir, config_after.target_dir, get_failure_message(args, sut))
|
||||
|
||||
def test_save_logging_disable(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_JOURNAL) as config_dir_path:
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_CONSOLE) as config_dir_path:
|
||||
config_file_path = self.__config_file_generator.get_config_file_path(config_dir_path)
|
||||
config_before = ConfigHandler.load_config_json(config_file_path)
|
||||
self.assertEqual('journal', config_before.log_to, f"Unexpected before config logging value: {config_before}")
|
||||
self.assertEqual('console', config_before.log_to, f"Unexpected before config logging value: {config_before}")
|
||||
args = ArgsHandler().parse_args(['-C', '+l', '--save', '-c', config_file_path])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
@@ -270,10 +269,10 @@ class ConfigHandlerTest(unittest.TestCase):
|
||||
self.assertEqual('disabled', config_after.log_to, get_failure_message(args, sut))
|
||||
|
||||
def test_save_logging_change(self):
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_JOURNAL) as config_dir_path:
|
||||
with self.__config_file_generator.create_config_file(Part.TARGETS, Part.LOG_CONSOLE) as config_dir_path:
|
||||
config_file_path = self.__config_file_generator.get_config_file_path(config_dir_path)
|
||||
config_before = ConfigHandler.load_config_json(config_file_path)
|
||||
self.assertEqual('journal', config_before.log_to, f"Unexpected before config logging value: {config_before}")
|
||||
self.assertEqual('console', config_before.log_to, f"Unexpected before config logging value: {config_before}")
|
||||
args = ArgsHandler().parse_args(['-C', '-l', '/tmp', '--save', '-c', config_file_path])
|
||||
name = inspect.currentframe().f_code.co_name
|
||||
sut = ConfigHandler(args, name)
|
||||
|
||||
Reference in New Issue
Block a user