Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues/180: make naz.Client.logger to be a python logging.Handler #181

Merged
merged 92 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
705f41c
m
komuw Nov 10, 2019
dab2dcc
m
komuw Nov 10, 2019
3020c8b
m
komuw Nov 23, 2019
3d7c94b
m
komuw Nov 23, 2019
d6a66ce
m
komuw Nov 23, 2019
ad13f4c
m
komuw Nov 23, 2019
22a01e7
m
komuw Nov 23, 2019
dff5eb8
m
komuw Nov 23, 2019
3a041ed
m
komuw Nov 23, 2019
e199743
m
komuw Nov 23, 2019
a717772
m
komuw Nov 23, 2019
fdd2212
m
komuw Nov 23, 2019
e0c3873
m
komuw Nov 23, 2019
c1f4995
m
komuw Nov 23, 2019
df80c02
m
komuw Nov 23, 2019
dcd7170
m
komuw Nov 23, 2019
a6567c9
m
komuw Nov 24, 2019
4fd4abf
m
komuw Nov 24, 2019
294d9c7
m
komuw Nov 24, 2019
06e66b4
m
komuw Nov 24, 2019
cf2ee4b
m
komuw Nov 24, 2019
5ac7740
m
komuw Nov 24, 2019
a17d161
m
komuw Nov 24, 2019
0a15455
m
komuw Nov 24, 2019
2f995a2
m
komuw Nov 24, 2019
a557a2d
m
komuw Nov 24, 2019
5d018ec
m
komuw Nov 24, 2019
0309fcc
m
komuw Nov 24, 2019
1367c0b
m
komuw Nov 24, 2019
999d7fe
m
komuw Nov 24, 2019
fd42ef7
m
komuw Nov 24, 2019
e01d388
m
komuw Nov 24, 2019
b327f06
m
komuw Nov 24, 2019
372f93c
m
komuw Nov 24, 2019
20cf90a
m
komuw Nov 24, 2019
8620d60
m
komuw Nov 24, 2019
4ea6256
m
komuw Nov 24, 2019
531a155
m
komuw Nov 24, 2019
8e9ac52
m
komuw Nov 24, 2019
732821c
m
komuw Nov 24, 2019
580780f
m
komuw Nov 24, 2019
eb529ad
m
komuw Nov 24, 2019
1b82433
m
komuw Nov 24, 2019
604589f
m
komuw Nov 24, 2019
caf23dd
m
komuw Nov 24, 2019
4ddc9cc
m
komuw Nov 24, 2019
94217b9
m
komuw Nov 24, 2019
0956a18
m
komuw Nov 24, 2019
c5a61bb
m
komuw Nov 24, 2019
36a82af
m
komuw Nov 24, 2019
60ede9b
ff
komuw Nov 24, 2019
df43607
m
komuw Nov 24, 2019
ea2c877
m
komuw Nov 24, 2019
70bb74c
m
komuw Nov 24, 2019
55d2040
m
komuw Nov 24, 2019
1d8b0af
Update fuzz.py
komuw Nov 24, 2019
c073475
m
komuw Nov 24, 2019
995b54d
m
komuw Nov 24, 2019
19aaa15
m
komuw Nov 24, 2019
b6ddb57
m
komuw Nov 24, 2019
436c244
m
komuw Nov 24, 2019
b3a61ad
m
komuw Nov 24, 2019
1aa4932
m
komuw Nov 24, 2019
f4f480e
m
komuw Nov 24, 2019
e0fd350
m
komuw Nov 24, 2019
c95a621
m
komuw Nov 24, 2019
f37ba3b
m
komuw Nov 25, 2019
09e20c1
m
komuw Nov 25, 2019
718757f
m
komuw Nov 25, 2019
6a8d9f9
m
komuw Nov 25, 2019
2ac3d84
m
komuw Nov 25, 2019
1499066
m
komuw Nov 25, 2019
6ded57b
m
komuw Nov 25, 2019
4b8574f
m
komuw Nov 25, 2019
d7e51c9
m
komuw Nov 25, 2019
ee25d17
m
komuw Nov 25, 2019
cb7e126
m
komuw Nov 25, 2019
47a279f
m
komuw Nov 25, 2019
3d9dcfb
m
komuw Nov 25, 2019
9f1accf
m
komuw Nov 25, 2019
4fae4e8
m
komuw Nov 25, 2019
b7b4b2e
m
komuw Nov 25, 2019
2a6bae0
m
komuw Nov 25, 2019
d27b089
m
komuw Nov 25, 2019
b4b6c5e
m
komuw Nov 25, 2019
a9d322f
m
komuw Nov 25, 2019
410f4f9
m
komuw Nov 25, 2019
433d393
m
komuw Nov 25, 2019
6bbe8a1
m
komuw Nov 25, 2019
92bedc4
m
komuw Nov 25, 2019
3a43c9a
m
komuw Nov 25, 2019
36f8d5c
m
komuw Nov 25, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ most recent version is listed first.
## **version:** v0.7.4
- Fix a bug that prevented `enquire_link` and `deliver_sm` from been sent: https://github.com/komuw/naz/pull/179
- Enforce `naz` message protocol in code : https://github.com/komuw/naz/pull/179
- make make `naz.Client.logger` to be a python `logging.Handler`: https://github.com/komuw/naz/pull/181


## **version:** v0.7.3
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ run:
```

**NB:**
(a) For more information about the `naz` config file, consult the [documentation here](https://komuw.github.io/naz/client.html)
(a) The ```naz`` config file(ie, the dotted path we pass in to ``naz-cli --client``) is any python file that has a `naz.Client instance <https://komuw.github.io/naz/client.html>`_ declared in it.
(b) More [examples can be found here](https://github.com/komuw/naz/tree/master/examples). As an example, start the SMSC simulator(`docker-compose up`) then in another terminal run, `naz-cli --client examples.example_config.client`

To see help:
Expand Down Expand Up @@ -198,9 +198,13 @@ So, for example if you wanted to annotate all log-events with a release version
```python
import naz

logger = naz.log.SimpleLogger(
"naz.client",
log_metadata={ "environment": "production", "release": "v5.6.8"}
)
cli = naz.Client(
...
log_metadata={ "environment": "production", "release": "canary"},
logger=logger,
)
```
and then these will show up in all log events.
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
smsc_port=2775,
system_id="smppclient1",
password=os.environ["SMSC_PASSWORD"],
loglevel="DEBUG",
log_metadata={"project": "naz_benchmarks"},
broker=MyRedisBroker(),
hook=BenchmarksHook(),
logger=BenchmarksLogger("naz.client"),
logger=BenchmarksLogger(
logger_name="naz.benchmarks", level="DEBUG", log_metadata={"project": "naz_benchmarks"},
),
socket_timeout=15.00,
enquire_link_interval=80.00,
address_range="^{0}".format(
Expand Down
55 changes: 13 additions & 42 deletions benchmarks/my_logger.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import os
import errno
import json
import typing
import logging

import naz


def makelog(log_directory="/usr/src/nazLog", log_file="naz_log_file"):
log_file = os.path.join(log_directory, log_file)
Expand Down Expand Up @@ -35,43 +32,17 @@ def makelog(log_directory="/usr/src/nazLog", log_file="naz_log_file"):
log_file = makelog()


class MyLogAdapter(naz.log._NazLoggingAdapter):
def process(self, msg, kwargs):
timestamp = self.formatTime()
if isinstance(msg, str):
merged_msg = "{0} {1} {2}".format(timestamp, msg, self.extra)
if self.extra == {}:
merged_msg = "{0} {1}".format(timestamp, msg)
return self._dumps(merged_msg), kwargs
else:
_timestamp = {"timestamp": timestamp}
merged_msg = {**_timestamp, **msg, **self.extra}
return self._dumps(merged_msg), kwargs

def _dumps(self, merged_msg):
try:
return json.dumps(merged_msg)
except Exception as e:
return self._dumps(str(e))


class BenchmarksLogger(naz.log.SimpleLogger):
def bind(self, level: typing.Union[str, int], log_metadata: dict) -> None:
level = self._nameToLevel(level=level)
self._logger = logging.getLogger(self.logger_name)
class BenchmarksLogger(logging.Logger):
def __init__(
self,
logger_name: str,
level: typing.Union[str, int] = logging.INFO,
log_metadata: typing.Union[None, dict] = None,
handler: typing.Union[None, logging.Handler] = None,
) -> None:
super(BenchmarksLogger, self).__init__(logger_name, level, log_metadata, handler)
formatter = logging.Formatter("%(message)s")

handler1 = logging.FileHandler(filename=log_file)
handler1.setFormatter(formatter)
handler1.setLevel(logging.DEBUG)
self._logger.addHandler(handler1)

# this may have been causing the issue mentioned at:
# - https://github.com/komuw/naz/issues/103#issuecomment-500208481
# handler2 = logging.StreamHandler()
# handler2.setFormatter(formatter)
# handler2.setLevel(level)
# self._logger.addHandler(handler2)

self._logger.setLevel(level)
self.logger: logging.LoggerAdapter = MyLogAdapter(self._logger, log_metadata)
handler2 = logging.FileHandler(filename=log_file)
handler2.setFormatter(formatter)
handler2.setLevel(self.level)
self.addHandler(handler2)
7 changes: 4 additions & 3 deletions benchmarks/smpp_n_broker_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ def __init__(
self.chaos = chaos

self.docker_client = docker.from_env()
self.logger = naz.log.SimpleLogger("naz_benchmarks.{0}".format(self.container_name))
self.logger.bind(
level="INFO", log_metadata={"container_name": self.container_name, "chaos": self.chaos}
self.logger = naz.log.SimpleLogger(
"naz_benchmarks.{0}".format(self.container_name),
level="INFO",
log_metadata={"container_name": self.container_name, "chaos": self.chaos},
)

self.container_max_run_duration: float = 16.00 # mins
Expand Down
2 changes: 1 addition & 1 deletion cli/utils/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import naz


def load_class(dotted_path: str, logger: naz.log.BaseLogger) -> naz.Client:
def load_class(dotted_path: str, logger: logging.Logger) -> naz.Client:
"""
taken from: https://github.com/coleifer/huey/blob/4138d454cc6fd4d252c9350dbd88d74dd3c67dcb/huey/utils.py#L44
huey is released under MIT license a copy of which can be found at: https://github.com/coleifer/huey/blob/master/LICENSE
Expand Down
4 changes: 2 additions & 2 deletions cli/utils/sig.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import naz


async def _signal_handling(logger: naz.log.BaseLogger, client: naz.Client) -> None:
async def _signal_handling(logger: logging.Logger, client: naz.Client) -> None:
try:
loop = asyncio.get_running_loop()
except RuntimeError:
Expand Down Expand Up @@ -34,7 +34,7 @@ async def _signal_handling(logger: naz.log.BaseLogger, client: naz.Client) -> No


async def _handle_termination_signal(
logger: naz.log.BaseLogger, _signal: "signal.Signals", client: naz.Client
logger: logging.Logger, _signal: "signal.Signals", client: naz.Client
) -> None:
logger.log(
logging.INFO,
Expand Down
Loading