Skip to content

Commit

Permalink
merging recent logging fixes into v2 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
bug-or-feature committed Apr 17, 2023
1 parent a3514e9 commit 258c74c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 14 deletions.
7 changes: 5 additions & 2 deletions examples/logging/poc.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@
keep_type.info("type first, no stage")

# alias logtoscreen
logtoscreen = logtoscreen("logtoscreen")
logtoscreen.info("logtoscreen() is a temporary alias for get_logger()")
screen_name = logtoscreen("logtoscreen")
screen_name.info("logtoscreen() is a temporary alias for get_logger()")

screen_attrs = logtoscreen(**{TYPE_LOG_LABEL: "config", STAGE_LOG_LABEL: "config"})
screen_attrs.info("logtoscreen() with attributes is a temporary alias for get_logger()")

# critical mail
level.critical("sends mail")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ norecursedirs = "examples"
addopts = "--doctest-modules --ignore=systems/provided/moretradingrules/temp.py"
log_cli = "true"
log_cli_level = 10
log_format = '%(asctime)s %(name)s %(levelname)s %(message)s'
log_format = '%(asctime)s %(levelname)s %(name)s %(message)s'
log_date_format = '%Y-%m-%d %H:%M:%S'
doctest_optionflags = ["ELLIPSIS", "NUMBER", "NORMALIZE_WHITESPACE"]
testpaths = [
Expand Down
21 changes: 17 additions & 4 deletions syslogging/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class DynamicAttributeLogger(logging.LoggerAdapter):
# TODO data.update_log(contract_object.specific_log(data.log))
# TODO data_blob._get_specific_logger
# TODO self.log.close_log_file()
# TODO log = logToFile
# TODO log: pst_logger = nullLog("")
# TODO log_with_attributes
# TODO log_level = "on"
# TODO system.set_logging_level(log_level)
"""

Expand Down Expand Up @@ -106,7 +106,10 @@ def label(self, **kwargs):
DeprecationWarning,
2,
)
attributes = {**self.extra, **kwargs}
if self.extra is None or len(self.extra) == 0:
attributes = {**kwargs}
else:
attributes = {**self.extra, **kwargs}
self._check_attributes(attributes)
self.extra = attributes

Expand All @@ -117,7 +120,10 @@ def setup_empty_except_keep_type(self):
DeprecationWarning,
2,
)
attributes = {TYPE_LOG_LABEL: self.extra[TYPE_LOG_LABEL]}
if self.extra is not None and TYPE_LOG_LABEL in self.extra:
attributes = {TYPE_LOG_LABEL: self.extra[TYPE_LOG_LABEL]}
else:
attributes = {}
return DynamicAttributeLogger(logging.getLogger(self.name), attributes)

def set_logging_level(self, new_log_level):
Expand All @@ -134,6 +140,13 @@ def set_logging_level(self, new_log_level):
else:
self.logger.setLevel(new_log_level)

def close_log_file(self):
warnings.warn(
"The 'close_log_file' function is deprecated, and does nothing anyway",
DeprecationWarning,
2,
)

def _check_attributes(self, attributes: dict):
if attributes is not None:
bad_attributes = get_list_of_disallowed_attributes(attributes)
Expand Down
32 changes: 30 additions & 2 deletions syslogging/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,42 @@
def get_logger(name, attributes=None):
if not syslogging.logging_configured:
_configure_logging()
if name is None or name == "":
if attributes is not None and "type" in attributes:
name = attributes["type"]
return DynamicAttributeLogger(logging.getLogger(name), attributes)


def logtoscreen(name):
def logtoscreen(name="", **kwargs):
warnings.warn(
"The 'logtoscreen' class is deprecated, "
"use get_logger() from syslogging.logger instead",
DeprecationWarning,
2,
)
if name is None or name == "":
if "type" in kwargs:
name = kwargs["type"]
return DynamicAttributeLogger(logging.getLogger(name), kwargs)


def nullLog(name):
warnings.warn(
"The 'nullLog' class is deprecated, "
"use get_logger() from syslogging.logger instead",
DeprecationWarning,
2,
)
return get_logger(name)


def logToFile(name, data):
warnings.warn(
"The 'logToFile' class is deprecated, "
"use get_logger() from syslogging.logger instead",
DeprecationWarning,
2,
)
return get_logger(name)


Expand All @@ -39,10 +65,12 @@ def _configure_sim():
handler.setLevel(logging.DEBUG)
logging.basicConfig(
handlers=[handler],
format="%(asctime)s %(name)s %(levelname)s %(message)s",
format="%(asctime)s %(levelname)s %(name)s %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
level=logging.DEBUG,
)
logging.getLogger("ib_insync").setLevel(logging.WARNING)
logging.getLogger("arctic").setLevel(logging.WARNING)
syslogging.logging_configured = True


Expand Down
7 changes: 6 additions & 1 deletion syslogging/logging_prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ objects:
maxsize: 1000
formatters:
simple:
format: '%(asctime)s %(name)s %(levelname)s %(message)s'
format: '%(asctime)s %(levelname)s %(name)s %(message)s'
datefmt: '%Y-%m-%d %H:%M:%S'
handlers:
console:
Expand All @@ -31,6 +31,11 @@ handlers:
- cfg://handlers.console
- cfg://handlers.file_rotating
- cfg://handlers.email
loggers:
ib_insync:
level: WARNING
arctic:
level: WARNING
root:
level: DEBUG
handlers:
Expand Down
7 changes: 6 additions & 1 deletion syslogging/logging_sim.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ version: 1
disable_existing_loggers: true
formatters:
simple:
format: '%(asctime)s %(name)s %(levelname)s %(message)s'
format: '%(asctime)s %(levelname)s %(name)s %(message)s'
dateformat: '%Y-%m-%d %H:%M:%S'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
loggers:
ib_insync:
level: WARNING
arctic:
level: WARNING
root:
level: DEBUG
handlers: [console]
40 changes: 37 additions & 3 deletions syslogging/tests/logging_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def test_attributes_bad(self):
with pytest.raises(Exception):
get_logger("my_type", {"foo": "bar"})

def test_no_name_no_attributes(self, caplog):
logger = get_logger("")
logger.warn("foo")
assert caplog.record_tuples == [("root", logging.WARNING, "foo")]

def test_attributes_clear(self, caplog):
clear = get_logger("Clear", {"stage": "first", "type": "one"})
clear.info("Clearing attributes", method="clear")
Expand Down Expand Up @@ -64,6 +69,10 @@ def test_setup(self):
assert logger.name == "my_type"
assert logger.extra["stage"] == "left"

no_attrs = get_logger("no_attrs")
no_attrs = no_attrs.setup(instrument_code="XYZ")
assert no_attrs.extra["instrument_code"] == "XYZ"

def test_setup_bad(self):
logger = get_logger("my_type", {"stage": "bar"})
with pytest.raises(Exception):
Expand All @@ -76,6 +85,10 @@ def test_label(self):
assert logger.extra["stage"] == "left"
assert logger.extra["instrument_code"] == "ABC"

no_attrs = get_logger("no_attrs")
no_attrs.label(instrument_code="XYZ")
assert no_attrs.extra["instrument_code"] == "XYZ"

def test_label_bad(self):
logger = get_logger("my_type", {"stage": "bar"})
with pytest.raises(Exception):
Expand Down Expand Up @@ -103,12 +116,33 @@ def test_warn(self, caplog):
]

def test_logtoscreen(self):
logger = logtoscreen("logtoscreen")
assert logger.name == "logtoscreen"
assert logger.getEffectiveLevel() == logging.DEBUG
screen = logtoscreen("logtoscreen")
assert screen.name == "logtoscreen"
assert screen.getEffectiveLevel() == logging.DEBUG

attrs = logtoscreen(**{TYPE_LOG_LABEL: "config", STAGE_LOG_LABEL: "config"})
assert attrs.name == "config"
assert attrs.getEffectiveLevel() == logging.DEBUG

def test_set_logging_level(self):
logger = get_logger("Set_Level")
assert logger.getEffectiveLevel() == logging.DEBUG
logger.set_logging_level(logging.INFO)
assert logger.getEffectiveLevel() == 20

def test_setup_empty_with(self):
setup_with = get_logger("Setup_With", {"type": "foo", "stage": "one"})
assert setup_with.extra["type"] == "foo"
assert setup_with.extra["stage"] == "one"
setup_with = setup_with.setup_empty_except_keep_type()
assert setup_with.extra["type"] == "foo"
assert "stage" not in setup_with.extra

def test_setup_empty_without(self):
setup_without = get_logger("Setup_Without", {"stage": "one"})
setup_without = setup_without.setup_empty_except_keep_type()
assert "type" not in setup_without.extra

setup_without = get_logger("Setup_Without")
setup_without = setup_without.setup_empty_except_keep_type()
assert "type" not in setup_without.extra

0 comments on commit 258c74c

Please sign in to comment.