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

MNT: Create loggers.py and add Elasticsearch handler #49

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[flake8]
exclude = .git,__pycache__,build,dist,versioneer.py,nslsii/_version.py,docs/source/conf.py
ignore = E402,W504
exclude = .git,__pycache__,doc/source/conf.py,build,dist,versioneer.py
max-line-length = 115
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My concern here is that the line length here is 115, but in .travis.yml it's set to 79 via command-line arg (which most likely has a priority). I think we should have a single source of truth here, so that only one configuration is controlling the settings in both development environments and in the CI systems.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why it was set to 79 in .travis.yml since that is the default anyway. @ke-zhang-rd, I think the right path here is to remove --max-line-length=79 from this line as part of this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the plan.

4 changes: 4 additions & 0 deletions nslsii/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from IPython import get_ipython
from ._version import get_versions
from .loggers import configure_elastic
__version__ = get_versions()['version']
del get_versions

Expand Down Expand Up @@ -148,6 +149,9 @@ def configure_base(user_ns, broker_name, *,
ch.setLevel(logging.ERROR)
ophyd.ophydobj.logger.addHandler(ch)

configure_elastic(broker_name)
#Configure elastic log handler including bluesky, carproto and ophyd

# convenience imports
# some of the * imports are for 'back-compatibility' of a sort -- we have
# taught BL staff to expect LiveTable and LivePlot etc. to be in their
Expand Down
2 changes: 1 addition & 1 deletion nslsii/common/ipynb/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def view_frame(i, vmin, vmax):

plt.show()

interact(view_frame, i=(0, n-1), vmin=minmax, vmax=minmax)
interact(view_frame, i=(0, n - 1), vmin=minmax, vmax=minmax)


def image_stack_to_movie(images, frames=None, vmin=None, vmax=None,
Expand Down
6 changes: 3 additions & 3 deletions nslsii/common/ipynb/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def get_sys_info():

mem = psutil.virtual_memory()
html += '<tr><td>Total System Memory</td>'
html += '<td>{:.4} Mb</td></td>'.format(mem.total/1024**3)
html += '<td>{:.4} Mb</td></td>'.format(mem.total / 1024**3)
html += '<tr><td>Total Memory Used</td>'
html += '<td>{:.4} Mb</td></td>'.format(mem.used/1024**3)
html += '<td>{:.4} Mb</td></td>'.format(mem.used / 1024**3)
html += '<tr><td>Total Memory Free</td>'
html += '<td>{:.4} Mb</td></td>'.format(mem.free/1024**3)
html += '<td>{:.4} Mb</td></td>'.format(mem.free / 1024**3)

html += '<tr><td>Number of CPU Cores</td><td>{}</td></tr>'.format(
psutil.cpu_count())
Expand Down
59 changes: 59 additions & 0 deletions nslsii/loggers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import logging
import requests
import json

class ElasticHandler(logging.Handler):
'''
Logging handler that submits data to Elastic

Parameters
----------
url: string
url to Elastic server

level: Logging Levels, optional
Handler logging level
'''
def __init__(self, url, level=logging.INFO):
self.url = url
self.level = level
super().__init__(self.level)

def emit(self, record):
'''
method is used to send the message to its destination and
logging for debug
'''
# Extract useful info from the record and put them into a dict.
try:
record_dict = {'name': record.name,
'levelname': record.levelname,
'pathname': record.pathname,
'lineno': record.lineno,
'msg': self.format(record),
'args': list(map(str, record.args)),
'threadName': record.threadName}

if hasattr(record, 'pv'): record_dict['pv'] = record.pv
if hasattr(record, 'address'): record_dict['address'] = record.address[0]+':'+str(record.address[1])
if hasattr(record, 'role'): record_dict['role'] = record.role
response = requests.post(self.url, json=record_dict)
response.raise_for_status()
except Exception:
self.handleError(record)

def configure_elastic(beamline):
url = 'http://elasticsearch.cs.nsls2.local/' + beamline + '.blueksy'
local_url = 'http://localhost:9200/test1/_doc'

bluesky_logger = logging.getLogger('bluesky')
bluesky_elastic_hdr = ElasticHandler(url, level = logging.DEBUG)
bluesky_logger.addHandler(elastic_hdr)

carproto_logger = logging.getLogger('carproto')
carproto_elastic_hdr = ElasticHandler(local_url, level = logging.DEBUG)
carproto_logger.addHandler(elastic_hdr)

ophyd_logger = logging.getLogger('ophyd')
ophyd_elastic_hdr = ElasticHandler(url, level = logging.INFO)
ophyd_logger.addHandler(elastic_hdr)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ numpy
ophyd
psutil
pyolog
requests