Skip to content

Commit 67391c4

Browse files
authored
Merge pull request #112 from Pennycook/logfile
Write logs to cbi.log by default
2 parents c400221 + 326fa8e commit 67391c4

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

codebasin/__main__.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from codebasin import CodeBase, config, finder, report, util
1414
from codebasin.walkers.platform_mapper import PlatformMapper
1515

16+
log = logging.getLogger("codebasin")
1617
version = "1.2.0"
1718

1819

@@ -144,12 +145,28 @@ def main():
144145

145146
args = parser.parse_args()
146147

147-
stdout_log = logging.StreamHandler(sys.stdout)
148-
stdout_log.setFormatter(logging.Formatter("[%(levelname)-8s] %(message)s"))
149-
logging.getLogger("codebasin").addHandler(stdout_log)
150-
logging.getLogger("codebasin").setLevel(
151-
max(1, logging.WARNING - 10 * (args.verbose - args.quiet)),
152-
)
148+
# Configure logging such that:
149+
# - All messages are written to a log file
150+
# - Only errors are written to the terminal by default
151+
# - Messages written to terminal are based on -q and -v flags
152+
formatter = logging.Formatter("[%(levelname)-8s] %(message)s")
153+
log.setLevel(logging.DEBUG)
154+
155+
file_handler = logging.FileHandler("cbi.log", mode="w")
156+
file_handler.setLevel(logging.INFO)
157+
file_handler.setFormatter(formatter)
158+
log.addHandler(file_handler)
159+
160+
# Inform the user that a log file has been created.
161+
# 'print' instead of 'log' to ensure the message is visible in the output.
162+
log_path = os.path.abspath("cbi.log")
163+
print(f"Log file created at {log_path}")
164+
165+
log_level = max(1, logging.ERROR - 10 * (args.verbose - args.quiet))
166+
stdout_handler = logging.StreamHandler(sys.stdout)
167+
stdout_handler.setLevel(log_level)
168+
stdout_handler.setFormatter(formatter)
169+
log.addHandler(stdout_handler)
153170

154171
# If no specific report was specified, generate all reports.
155172
# Handled here to prevent "all" always being in the list.
@@ -243,5 +260,5 @@ def report_enabled(name):
243260
sys.argv[0] = "codebasin"
244261
main()
245262
except Exception as e:
246-
logging.getLogger("codebasin").error(str(e))
263+
log.error(str(e))
247264
sys.exit(1)

0 commit comments

Comments
 (0)