Skip to content

Commit

Permalink
Merge pull request #63 from Rajendra-R/expose_logger
Browse files Browse the repository at this point in the history
#61 exposed init_logger and get_logger
  • Loading branch information
Ravipudi Sai Rajendra Prasad authored Mar 28, 2018
2 parents c977932 + 00c64ae commit c46d822
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
45 changes: 41 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,29 @@ Run the script as follows and observe the usage information shown. Note how the
description appears along with the `c` argument.
```bash
python adder.py --help
usage: adder.py [-h] [--name NAME] [--log LOG] [--log-level LOG_LEVEL]
[--quiet]
usage: adder.py [-h] [--name NAME] [--log-level LOG_LEVEL]
[--log-format {json,pretty}] [--log-file LOG_FILE] [--quiet]
[--metric-grouping-interval METRIC_GROUPING_INTERVAL]
[--debug]
{run} ...

Adds numbers

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

commands:
{run}
Expand Down Expand Up @@ -137,3 +147,30 @@ https://docs.python.org/2/library/logging.html#logging-levels.
`log` is a log object created using python's standard `logging` module. You can
read more about it at https://docs.python.org/2/library/logging.html.
### Metric-Grouping
When writing a `Metric` using `self.log`, you can specify `type=metric`. If this is done, a background thread will automatically group multiple metrics into one by averaging values (to prevent writing too many log lines).
test.py
```
from basescript import BaseScript
import time
import random
class Stats(BaseScript):
def __init__(self):
super(Stats, self).__init__()
def run(self):
ts = time.time()
while True:
# Metric Format.
self.log.info("stats", time_duration=(time.time()-ts), type="metric", random_number=random.randint(1, 50))
if __name__ == '__main__':
Stats().start()
```
Run the command to see the output.
```
python test.py --metric-grouping-interval 5 run
```
1 change: 1 addition & 0 deletions basescript/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import absolute_import

from .basescript import BaseScript
from .log import init_logger, get_logger
3 changes: 1 addition & 2 deletions basescript/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ def _proxy_to_logger(self, method_name, event, *event_args,
event=event,
**event_kw)

#
# Pass-through methods to mimick the stdlib's logger interface.
#

def setLevel(self, level):
"""
Expand Down Expand Up @@ -260,6 +258,7 @@ def define_log_renderer(fmt, fpath, quiet):

def _structlog_default_keys_processor(logger_class, log_method, event):
''' Add unique id, type and hostname '''
global HOSTNAME

if 'id' not in event:
event['id'] = '%s_%s' % (
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_long_description():

long_description = get_long_description()

version = '0.2.3'
version = '0.2.4'
setup(
name="basescript",
version=version,
Expand Down

0 comments on commit c46d822

Please sign in to comment.