-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #257 from phenobarbital/logstash-handler
Logstash Handler Configuration (working example)
- Loading branch information
Showing
5 changed files
with
75 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .logstash import LogstashHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# basic configuration of Logstash | ||
try: | ||
import logstash_async # pylint: disable=W0611 | ||
except ImportError as ex: | ||
raise RuntimeError( | ||
"NavConfig: Logstash Logging is enabled but Logstash async \ | ||
dependency is not installed.\ | ||
Hint: run 'pip install logstash_async'." | ||
) from ex | ||
|
||
|
||
class LogstashHandler: | ||
def __init__(self, config) -> None: | ||
self.env = config.ENV if config.ENV is not None else 'production' | ||
self.host = config.get('LOGSTASH_HOST', fallback='127.0.0.1') | ||
self.port = config.getint('LOGSTASH_PORT', fallback=6000) | ||
|
||
def formatter( | ||
self, | ||
application: str, | ||
path: str, | ||
fqdn: bool = False, | ||
**kwargs | ||
): | ||
return { | ||
'()': 'logstash_async.formatter.LogstashFormatter', | ||
'message_type': 'python-logstash', | ||
'fqdn': fqdn, | ||
"extra_prefix": 'dev', | ||
'extra': { | ||
'application': f'{application}', | ||
'project_path': f'{path}', | ||
'environment': self.env, | ||
**kwargs | ||
} | ||
} | ||
|
||
def handler(self, loglevel, enable_localdb: bool = False, **kwargs): | ||
hdlr = { | ||
'class': 'logstash_async.handler.AsynchronousLogstashHandler', | ||
'formatter': 'logstash', | ||
'transport': 'logstash_async.transport.TcpTransport', | ||
'host': self.host, | ||
'port': int(self.port), | ||
'level': loglevel | ||
} | ||
if enable_localdb is True: | ||
log_dir = kwargs['logdir'] | ||
hdlr['database_path'] = f'{log_dir}/logstash.db' | ||
else: | ||
hdlr['database_path'] = '' | ||
if 'propagate' in kwargs: | ||
hdlr['propagate'] = kwargs['propagate'] | ||
return hdlr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
__title__ = 'navconfig' | ||
__description__ = ('Configuration tool for all Navigator Services ' | ||
'Tool for accessing Config info from different sources.') | ||
__version__ = '1.2.1' | ||
__version__ = '1.2.2' | ||
__author__ = 'Jesus Lara' | ||
__author_email__ = '[email protected]' | ||
__license__ = 'MIT' |