Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 273158e

Browse files
committed
Support logging to file
Required for PyEthApp 1.0.6
1 parent 480cb03 commit 273158e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

ethereum/slogging.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
import json
3-
from logging import StreamHandler, Formatter
3+
from logging import StreamHandler, Formatter, FileHandler
44
from ethereum.utils import bcolors, isnumeric
55

66

@@ -9,6 +9,7 @@
99
JSON_FORMAT = '%(message)s'
1010

1111
PRINT_FORMAT = '%(levelname)s:%(name)s\t%(message)s'
12+
FILE_PREFIX = '%(asctime)s'
1213

1314
TRACE = 5
1415

@@ -193,7 +194,7 @@ def getLogger(name=None):
193194
return rootLogger
194195

195196

196-
def configure(config_string=None, log_json=False):
197+
def configure(config_string=None, log_json=False, log_file=None):
197198
if not config_string:
198199
config_string = ":{}".format(DEFAULT_LOGLEVEL)
199200

@@ -209,6 +210,12 @@ def configure(config_string=None, log_json=False):
209210
formatter = Formatter(log_format)
210211
handler.setFormatter(formatter)
211212
rootLogger.addHandler(handler)
213+
if log_file:
214+
if not any(isinstance(hndlr, FileHandler) for hndlr in rootLogger.handlers):
215+
handler = FileHandler(log_file)
216+
formatter = Formatter("{} {}".format(FILE_PREFIX, log_format))
217+
handler.setFormatter(formatter)
218+
rootLogger.addHandler(handler)
212219

213220
# Reset logging levels before applying new config below
214221
for name, logger in SLogger.manager.loggerDict.items():

0 commit comments

Comments
 (0)