2424from .helpers .print_utils import PrintHelper
2525from .log_client import LogClient
2626
27+ # environment defaults
28+ CWD = os .environ ["PWD" ]
29+ USER = os .environ ["USER" ]
30+
31+ # ML_Logger defaults
32+ ROOT = os .environ .get ("ML_LOGGER_ROOT" , CWD )
33+ USER = os .environ .get ("ML_LOGGER_USER" , USER )
34+ ACCESS_TOKEN = os .environ .get ("ML_LOGGER_ACCESS_TOKEN" , None )
35+
2736
2837def pJoin (* args ):
2938 from os .path import join
@@ -198,7 +207,9 @@ def __repr__(self):
198207 # noinspection PyInitNewSignature
199208 # todo: use prefixes as opposed to prefix. (add *prefixae after prefix=None)
200209 # todo: resolve path segment with $env variables.
201- def __init__ (self , root_dir : str = None , prefix = None , * prefixae , buffer_size = 2048 , max_workers = None ,
210+ def __init__ (self , prefix = "" , * prefixae ,
211+ log_dir = ROOT , user = USER , access_token = ACCESS_TOKEN ,
212+ buffer_size = 2048 , max_workers = None ,
202213 asynchronous = None , summary_cache_opts : dict = None ):
203214 """ logger constructor.
204215
@@ -213,8 +224,11 @@ def __init__(self, root_dir: str = None, prefix=None, *prefixae, buffer_size=204
213224 | 1. prefix="causal_infogan" => logs to "/tmp/some_dir/causal_infogan"
214225 | 2. prefix="" => logs to "/tmp/some_dir"
215226
216- :param root_dir: the server host and port number
217227 :param prefix: the prefix path
228+ :param **prefixae: the rest of the prefix arguments
229+ :param log_dir: the server host and port number
230+ :param user: environment $ML_LOGGER_USER
231+ :param access_token: environment $ML_LOGGER_ACCESS_TOKEN
218232 :param asynchronous: When this is not None, we create a http thread pool.
219233 :param buffer_size: The string buffer size for the print buffer.
220234 :param max_workers: the number of request-session workers for the async http requests.
@@ -236,18 +250,19 @@ def __init__(self, root_dir: str = None, prefix=None, *prefixae, buffer_size=204
236250 self .summary_caches = defaultdict (partial (SummaryCache , ** (summary_cache_opts or {})))
237251
238252 # todo: add https support
239- self .root_dir = interpolate (root_dir ) or "/"
240- self .prefix = interpolate (prefix ) or os .getcwd ()[1 :]
241- if prefix is not None :
242- self .prefix = os .path .join (* [interpolate (p ) for p in (prefix , * prefixae ) if p is not None ])
253+ self .root_dir = interpolate (log_dir ) or ROOT
243254
244- # logger client contains thread pools, should not be re-created lightly.
245- self .client = LogClient (url = self .root_dir , asynchronous = asynchronous , max_workers = max_workers )
255+ prefixae = [interpolate (p ) for p in (prefix or "" , * prefixae ) if p is not None ]
256+ self .prefix = os .path .join (* prefixae ) if prefixae else ""
257+ self .client = LogClient (root = self .root_dir , user = user , access_token = access_token ,
258+ asynchronous = asynchronous , max_workers = max_workers )
246259
247260 def configure (self ,
248- root_dir : str = None ,
249261 prefix = None ,
250262 * prefixae ,
263+ log_dir : str = None ,
264+ user = None ,
265+ access_token = None ,
251266 asynchronous = None ,
252267 max_workers = None ,
253268 buffer_size = None ,
@@ -293,8 +308,11 @@ def configure(self,
293308 todo: the table at the moment seems a bit verbose. I'm considering making this
294309 just a single line print.
295310
296- :param log_directory:
297- :param prefix:
311+ :param prefix: the first prefix
312+ :param *prefixae: a list of prefix segments
313+ :param log_dir:
314+ :param user:
315+ :param access_token:
298316 :param buffer_size:
299317 :param summary_cache_opts:
300318 :param asynchronous:
@@ -305,9 +323,11 @@ def configure(self,
305323 """
306324
307325 # path logic
308- root_dir = interpolate (root_dir ) or os .getcwd ()
326+ log_dir = interpolate (log_dir ) or os .getcwd ()
309327 if prefix is not None :
310- self .prefix = os .path .join (* [interpolate (p ) for p in (prefix , * prefixae ) if p is not None ])
328+ prefixae = [interpolate (p ) for p in (prefix , * prefixae ) if p is not None ]
329+ if prefixae is not None :
330+ self .prefix = os .path .join (* prefixae )
311331
312332 if buffer_size is not None :
313333 self .print_buffer_size = buffer_size
@@ -318,17 +338,18 @@ def configure(self,
318338 self .summary_caches .clear ()
319339 self .summary_caches = defaultdict (partial (SummaryCache , ** (summary_cache_opts or {})))
320340
321- if root_dir != self .root_dir or asynchronous is not None or max_workers is not None :
322- # note: logger.configure shouldn't be called too often, so it is okay to assume
323- # that we can discard the old logClient.
324- # To quickly switch back and forth between synchronous and asynchronous calls,
325- # use the `SyncContext` and `AsyncContext` instead.
341+ if log_dir :
342+ self .root_dir = interpolate (log_dir ) or ROOT
343+ if log_dir or asynchronous is not None or max_workers is not None :
344+ # note: logger.configure shouldn't be called too often. To quickly switch back
345+ # and forth between synchronous and asynchronous calls, use the `SyncContext`
346+ # and `AsyncContext` instead.
326347 if not silent :
327- cprint ('creating new logging client...' , color = 'yellow' , end = ' ' )
328- self .root_dir = root_dir
329- self . client . __init__ ( url = self . root_dir , asynchronous = asynchronous , max_workers = max_workers )
348+ cprint ('creating new logging client...' , color = 'yellow' , end = '\r ' )
349+ self .client . __init__ ( root = self . root_dir , user = user , access_token = access_token ,
350+ asynchronous = asynchronous , max_workers = max_workers )
330351 if not silent :
331- cprint ('✓ done ' , color = "green" )
352+ cprint ('✓ created a new logging client ' , color = "green" )
332353
333354 if not silent :
334355 from urllib .parse import quote
0 commit comments