2020-11-06 17:23:14 +00:00
|
|
|
# piker: trading gear for hackers
|
|
|
|
# Copyright (C) 2018-present Tyler Goodlet (in stewardship of piker0)
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-01-20 18:21:06 +00:00
|
|
|
"""
|
|
|
|
Log like a forester!
|
|
|
|
"""
|
|
|
|
import logging
|
2018-01-26 16:31:11 +00:00
|
|
|
import json
|
2020-02-18 21:09:34 +00:00
|
|
|
|
|
|
|
import tractor
|
2018-01-26 16:31:11 +00:00
|
|
|
from pygments import highlight, lexers, formatters
|
2018-01-20 18:21:06 +00:00
|
|
|
|
2020-09-23 00:57:37 +00:00
|
|
|
# Makes it so we only see the full module name when using ``__name__``
|
|
|
|
# without the extra "piker." prefix.
|
2022-05-21 15:45:16 +00:00
|
|
|
_proj_name: str = 'piker'
|
2018-01-20 18:21:06 +00:00
|
|
|
|
|
|
|
|
2022-05-21 15:45:16 +00:00
|
|
|
def get_logger(
|
|
|
|
name: str = None,
|
|
|
|
|
|
|
|
) -> logging.Logger:
|
2018-01-20 18:21:06 +00:00
|
|
|
'''Return the package log or a sub-log for `name` if provided.
|
|
|
|
'''
|
2020-02-18 21:09:34 +00:00
|
|
|
return tractor.log.get_logger(name=name, _root_name=_proj_name)
|
2018-01-20 18:21:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_console_log(level: str = None, name: str = None) -> logging.Logger:
|
|
|
|
'''Get the package logger and enable a handler which writes to stderr.
|
|
|
|
|
|
|
|
Yeah yeah, i know we can use ``DictConfig``. You do it...
|
|
|
|
'''
|
2020-02-18 21:09:34 +00:00
|
|
|
return tractor.log.get_console_log(
|
|
|
|
level, name=name, _root_name=_proj_name) # our root logger
|
2018-01-26 16:31:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
def colorize_json(data, style='algol_nu'):
|
|
|
|
"""Colorize json output using ``pygments``.
|
|
|
|
"""
|
|
|
|
formatted_json = json.dumps(data, sort_keys=True, indent=4)
|
|
|
|
return highlight(
|
|
|
|
formatted_json, lexers.JsonLexer(),
|
|
|
|
# likeable styles: algol_nu, tango, monokai
|
|
|
|
formatters.TerminalTrueColorFormatter(style=style)
|
|
|
|
)
|