Skip to content

Commit 7c9a238

Browse files
authored
Merge pull request PSLmodels#1052 from jdebacker/centralize-logging-config
Centralize logging configuration
2 parents 938073b + 1bed023 commit 7c9a238

3 files changed

Lines changed: 49 additions & 23 deletions

File tree

ogcore/SS.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ogcore import tax, pensions, household, firm, utils, fiscal
77
from ogcore import aggregates as aggr
88
from ogcore.constants import SHOW_RUNTIME
9+
from ogcore import config
910
import os
1011
import warnings
1112
import logging
@@ -24,16 +25,6 @@
2425
"""
2526
ENFORCE_SOLUTION_CHECKS = True
2627

27-
"""
28-
Set flag for verbosity
29-
"""
30-
VERBOSE = True
31-
# Configure logging
32-
log_level = logging.INFO if VERBOSE else logging.WARNING
33-
logging.basicConfig(
34-
level=log_level, format="%(message)s" # Only show the message itself
35-
)
36-
3728

3829
"""
3930
------------------------------------------------------------------------
@@ -1235,24 +1226,27 @@ def SS_fsolve(guesses, *args):
12351226
+ list(error_BQ)
12361227
+ [error_TR]
12371228
)
1238-
logging.info(f"GE loop errors = {errors}")
1229+
logging.info(f"GE loop errors = ", [f"{error:.3e}" for error in errors])
12391230

12401231
return errors
12411232

12421233

1243-
def run_SS(p, client=None):
1234+
def run_SS(p, client=None, verbose=False):
12441235
"""
12451236
Solve for steady-state equilibrium of OG-Core.
12461237
12471238
Args:
12481239
p (OG-Core Specifications object): model parameters
12491240
client (Dask client object): client
1241+
verbose (bool): if True, set logging to INFO level
12501242
12511243
Returns:
12521244
output (dictionary): dictionary with steady-state solution
12531245
results
12541246
12551247
"""
1248+
# Configure logging level based on verbose parameter
1249+
config.set_logging_level(verbose)
12561250
# Create list of deviation factors for initial guesses of r and TR
12571251
dev_factor_list = [
12581252
[1.00, 1.0],

ogcore/TPI.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from ogcore import tax, utils, household, firm, fiscal
1919
from ogcore import aggregates as aggr
2020
from ogcore.constants import SHOW_RUNTIME
21+
from ogcore import config
2122
import os
2223
import warnings
2324
import logging
@@ -36,16 +37,6 @@
3637
"""
3738
ENFORCE_SOLUTION_CHECKS = True
3839

39-
"""
40-
Set flag for verbosity
41-
"""
42-
VERBOSE = True
43-
# Configure logging
44-
log_level = logging.INFO if VERBOSE else logging.WARNING
45-
logging.basicConfig(
46-
level=log_level, format="%(message)s" # Only show the message itself
47-
)
48-
4940

5041
def get_initial_SS_values(p):
5142
"""
@@ -558,19 +549,22 @@ def inner_loop(guesses, outer_loop_vars, initial_values, ubi, j, ind, p):
558549
return euler_errors, b_mat, n_mat
559550

560551

561-
def run_TPI(p, client=None):
552+
def run_TPI(p, client=None, verbose=False):
562553
"""
563554
Solve for transition path equilibrium of OG-Core.
564555
565556
Args:
566557
p (OG-Core Specifications object): model parameters
567558
client (Dask client object): client
559+
verbose (bool): if True, set logging to INFO level
568560
569561
Returns:
570562
output (dictionary): dictionary with transition path solution
571563
results
572564
573565
"""
566+
# Configure logging level based on verbose parameter
567+
config.set_logging_level(verbose)
574568
# unpack tuples of parameters
575569
initial_values, ss_vars, theta, baseline_values = get_initial_SS_values(p)
576570
(B0, b_sinit, b_splus1init, factor, initial_b, initial_n) = initial_values

ogcore/config.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Centralized configuration for OG-Core logging.
3+
4+
This module provides a centralized way to configure logging levels
5+
across all OG-Core modules, avoiding circular import issues.
6+
"""
7+
8+
import logging
9+
10+
VERBOSE = False # Default value
11+
12+
13+
def set_logging_level(verbose=False):
14+
"""
15+
Set the logging level for OG-Core modules.
16+
17+
Args:
18+
verbose (bool): If True, set logging to INFO level.
19+
If False, set logging to WARNING level.
20+
21+
Returns:
22+
int: The logging level that was set.
23+
"""
24+
global VERBOSE
25+
VERBOSE = verbose
26+
level = logging.INFO if verbose else logging.WARNING
27+
28+
# Configure the root logger
29+
logging.basicConfig(
30+
level=level,
31+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
32+
)
33+
34+
# Also set the ogcore logger specifically
35+
logger = logging.getLogger("ogcore")
36+
logger.setLevel(level)
37+
38+
return level

0 commit comments

Comments
 (0)