Skip to content

Commit

Permalink
implements #127: separate log levels
Browse files Browse the repository at this point in the history
- new config argument loglevel_file, defaults to same as loglevel
- include SpiceEV warnings if log level is DEBUG
  • Loading branch information
stefan.schirmeister committed Aug 2, 2023
1 parent fabff0b commit 2017e61
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions data/examples/ebus_toolbox.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ loglevel = INFO
# set to null to disable logfile creation
# leave empty to have default [timestamp].log
logfile =
# can set different log level for log file
loglevel_file =

##### SIMULATION PARAMETERS #####
# Maximum number of days to simulate, if not set simulate entire schedule
Expand Down
10 changes: 8 additions & 2 deletions ebus_toolbox/schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,14 @@ def run(self, args):
scenario = self.generate_scenario(args)

logging.info("Running SpiceEV...")
with warnings.catch_warnings():
warnings.simplefilter('ignore', UserWarning)
if logging.root.level > logging.DEBUG:
# don't log SpiceEV warnings for log levels above debug
# logging.root.level is lowest of console and file (if present)
with warnings.catch_warnings():
warnings.simplefilter('ignore', UserWarning)
scenario.run('distributed', vars(args).copy())
else:
# debug: log SpiceEV warnings as well
scenario.run('distributed', vars(args).copy())
assert scenario.step_i == scenario.n_intervals, \
'SpiceEV simulation aborted, see above for details'
Expand Down
16 changes: 13 additions & 3 deletions ebus_toolbox/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,10 @@ def nd_interp(input_values, lookup_table):
def setup_logging(args, time_str):
# set up logging
# always to console
log_handlers = [logging.StreamHandler()]
log_level = vars(logging)[args.loglevel]
console = logging.StreamHandler()
console.setLevel(log_level)
log_handlers = [console]
if args.logfile is not None:
# optionally to file in output dir
if args.logfile:
Expand All @@ -230,9 +233,13 @@ def setup_logging(args, time_str):
log_name = f"{time_str}.log"
log_path = args.output_directory / log_name
print(f"Writing log to {log_path}")
log_handlers.append(logging.FileHandler(log_path, encoding='utf-8'))
file_logger = logging.FileHandler(log_path, encoding='utf-8')
log_level_file = vars(logging).get(args.loglevel_file or args.loglevel)
file_logger.setLevel(log_level_file)
log_handlers.append(file_logger)
log_level = min(log_level, log_level_file)
logging.basicConfig(
level=vars(logging)[args.loglevel],
level=log_level,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=log_handlers
)
Expand Down Expand Up @@ -338,6 +345,9 @@ def get_args():
parser.add_argument('--loglevel', default='INFO',
choices=logging._nameToLevel.keys(), help='Log level.')
parser.add_argument('--logfile', default='', help='Log file suffix. null: no log file.')
parser.add_argument('--loglevel_file', default='',
choices=list(logging._nameToLevel.keys()) + [''],
help='Log level for file logger.')

# #### SpiceEV PARAMETERS ONLY DEFAULT VALUES NOT IN eBus-Toolbox CONFIG #####
parser.add_argument('--seed', default=1, type=int, help='set random seed')
Expand Down

0 comments on commit 2017e61

Please sign in to comment.