__init__.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. import sys
  3. from pathlib import Path
  4. import pathlib
  5. import logging
  6. _simple_format = logging.Formatter('{asctime} [{levelname:7s}] {name} {message}',
  7. datefmt='%Y-%m-%d %H:%M:%S',
  8. style='{')
  9. _initialized = False
  10. data_dir = Path('data')
  11. tgt_dir = Path('target')
  12. bin_dir = tgt_dir / 'release'
  13. bdtool = bin_dir / 'bookdata'
  14. def setup(debug=False):
  15. global _initialized
  16. ch = logging.StreamHandler(sys.stderr)
  17. ch.setLevel(logging.DEBUG if debug else logging.INFO)
  18. ch.setFormatter(_simple_format)
  19. root = logging.getLogger()
  20. root.addHandler(ch)
  21. root.setLevel(logging.INFO)
  22. logging.getLogger('dvc').setLevel(logging.ERROR)
  23. logging.getLogger('lenskit').setLevel(logging.DEBUG)
  24. logging.getLogger('').setLevel(logging.DEBUG)
  25. root.debug('log system configured')
  26. _initialized = True
  27. def script_log(name, debug=False):
  28. """
  29. Initialize logging and get a logger for a script.
  30. Args:
  31. name(str): The ``__name__`` of the script being run.
  32. debug(bool): whether to enable debug logging to the console
  33. """
  34. if not _initialized:
  35. setup(debug)
  36. logger = logging.getLogger(name)
  37. return logger