@@ -67,14 +67,18 @@ def __init__(
6767 log_conf : Optional [Path ] = None ,
6868 out_file : Optional [Path ] = None ,
6969 formatter : str = "%(asctime)s %(processName)s-%(name)s:%(levelname)s:%(message)s" ,
70+ console_only : bool = False ,
7071 ):
7172 self ._queues : List [Queue ] = []
7273 self ._log_conf_file = log_conf
7374 self ._log_output_file = out_file
7475 self ._formatter = formatter
76+ self ._console_only = console_only
7577 self ._proc : Optional [Process ] = None
7678
7779 self .apply_config ()
80+ if console_only :
81+ logging .getLogger ().setLevel (logging .DEBUG )
7882
7983 def apply_config (self ):
8084 if self ._log_conf_file is not None :
@@ -94,7 +98,7 @@ def start(self, initial_queue: Optional[Queue] = None):
9498 self ._proc = Process (
9599 target = self ._run_sink ,
96100 args = (
97- self ._log_output_file ,
101+ None if self . _console_only else self ._log_output_file ,
98102 self ._queues ,
99103 ),
100104 )
@@ -109,7 +113,7 @@ def join(self):
109113 self ._queues [0 ].put (None )
110114 self ._proc .join ()
111115
112- def _run_sink (self , output : Path , queues : List [Queue ]):
116+ def _run_sink (self , output : Optional [ Path ] , queues : List [Queue ]):
113117 """
114118 This is the process that consumes every log message (sink)
115119
@@ -124,15 +128,22 @@ def _run_sink(self, output: Path, queues: List[Queue]):
124128 for hdlr in hdlrs :
125129 rLogger .removeHandler (hdlr )
126130
127- # Set maxBytes to 50MB (50 * 1024 * 1024 bytes) and keep 5 backup files
128- h = logging .handlers .RotatingFileHandler (
129- output , maxBytes = 50 * 1024 * 1024 , backupCount = 5 , encoding = "utf-8"
130- )
131+ if output is None :
132+ import sys
133+
134+ h : logging .Handler = logging .StreamHandler (sys .stderr )
135+ rLogger .setLevel (logging .DEBUG )
136+ rLogger .warning ("Starting logging process (console only, no file output)" )
137+ else :
138+ # Set maxBytes to 50MB (50 * 1024 * 1024 bytes) and keep 5 backup files
139+ h = logging .handlers .RotatingFileHandler (
140+ output , maxBytes = 50 * 1024 * 1024 , backupCount = 5 , encoding = "utf-8"
141+ )
142+ rLogger .warning ("Starting logging process" )
143+ rLogger .warning ("Logging to %s" , output )
131144 f = logging .Formatter (self ._formatter )
132145 h .setFormatter (f )
133146 rLogger .addHandler (h )
134- rLogger .warning ("Starting logging process" )
135- rLogger .warning ("Logging to %s" , output )
136147
137148 # import logging_tree
138149 # logging_tree.printout()
@@ -172,6 +183,8 @@ def configurer(queue: Queue):
172183 This method needs to be called once in each process, so that log records get forwarded to the single process writing
173184 log messages.
174185 """
186+ import os
187+
175188 assert queue is not None , "You passed a None to configurer! You cannot do that"
176189 assert isinstance (
177190 queue , multiprocessing .queues .Queue
@@ -182,8 +195,11 @@ def configurer(queue: Queue):
182195 config = json5 .load (logconf )
183196 logging .config .dictConfig (config )
184197
185- h = logging .handlers .QueueHandler (queue )
186198 root = logging .getLogger ()
199+ if os .environ .get ("PIFINDER_DEBUG_NO_FILE_LOGS" ):
200+ root .setLevel (logging .DEBUG )
201+
202+ h = logging .handlers .QueueHandler (queue )
187203 root .addHandler (h )
188204
189205 def set_log_conf_file (self , config : Path ) -> None :
0 commit comments