Skip to content

Commit 7e847b5

Browse files
author
root
committed
#61 exposed init_logger and get_logger
1 parent c977932 commit 7e847b5

4 files changed

Lines changed: 44 additions & 7 deletions

File tree

README.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,29 @@ Run the script as follows and observe the usage information shown. Note how the
9090
description appears along with the `c` argument.
9191
```bash
9292
python adder.py --help
93-
usage: adder.py [-h] [--name NAME] [--log LOG] [--log-level LOG_LEVEL]
94-
[--quiet]
93+
usage: adder.py [-h] [--name NAME] [--log-level LOG_LEVEL]
94+
[--log-format {json,pretty}] [--log-file LOG_FILE] [--quiet]
95+
[--metric-grouping-interval METRIC_GROUPING_INTERVAL]
96+
[--debug]
9597
{run} ...
9698

9799
Adds numbers
98100

99101
optional arguments:
100102
-h, --help show this help message and exit
101103
--name NAME Name to identify this instance
102-
--log LOG Name of log file
103104
--log-level LOG_LEVEL
104105
Logging level as picked from the logging module
105-
--quiet
106+
--log-format {json,pretty}
107+
Force the format of the logs. By default, if the
108+
command is from a terminal, print colorful logs.
109+
Otherwise print json.
110+
--log-file LOG_FILE Writes logs to log file if specified, default: None
111+
--quiet if true, does not print logs to stderr, default: False
112+
--metric-grouping-interval METRIC_GROUPING_INTERVAL
113+
To group metrics based on time interval ex:10 i.e;(10
114+
sec)
115+
--debug To run the code in debug mode
106116

107117
commands:
108118
{run}
@@ -137,3 +147,30 @@ https://docs.python.org/2/library/logging.html#logging-levels.
137147
138148
`log` is a log object created using python's standard `logging` module. You can
139149
read more about it at https://docs.python.org/2/library/logging.html.
150+
151+
### Metric-Grouping
152+
We can collect the Metrics based on time interval.
153+
test.py
154+
```
155+
from basescript import BaseScript
156+
import time
157+
import random
158+
159+
class Stats(BaseScript):
160+
def __init__(self):
161+
super(Stats, self).__init__()
162+
163+
def run(self):
164+
ts = time.time()
165+
while True:
166+
# New Metric Format.
167+
self.log.info("stats", time_duration=(time.time()-ts), type="metric", random_number=random.randint(1, 50))
168+
169+
if __name__ == '__main__':
170+
Stats().start()
171+
```
172+
173+
Run the command to see the output.
174+
```
175+
python test.py --metric-grouping-interval 5 run
176+
```

basescript/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
from __future__ import absolute_import
22

33
from .basescript import BaseScript
4+
from .log import init_logger, get_logger

basescript/log.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,7 @@ def _proxy_to_logger(self, method_name, event, *event_args,
227227
event=event,
228228
**event_kw)
229229

230-
#
231230
# Pass-through methods to mimick the stdlib's logger interface.
232-
#
233231

234232
def setLevel(self, level):
235233
"""
@@ -260,6 +258,7 @@ def define_log_renderer(fmt, fpath, quiet):
260258

261259
def _structlog_default_keys_processor(logger_class, log_method, event):
262260
''' Add unique id, type and hostname '''
261+
global HOSTNAME
263262

264263
if 'id' not in event:
265264
event['id'] = '%s_%s' % (

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_long_description():
2222

2323
long_description = get_long_description()
2424

25-
version = '0.2.3'
25+
version = '0.2.4'
2626
setup(
2727
name="basescript",
2828
version=version,

0 commit comments

Comments
 (0)