Skip to content

Commit 38ce7fa

Browse files
committed
import
0 parents  commit 38ce7fa

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

.gitignore

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile ~/.gitignore_global
6+
7+
# Ignore bundler config
8+
/.bundle
9+
10+
# Ignore the build directory
11+
/build
12+
/dist
13+
*.egg-info
14+
*.deb
15+
16+
# Ignore Sass' cache
17+
/.sass-cache
18+
19+
# python compiled files
20+
*.pyc

MANIFEST.in

Whitespace-only changes.

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
logstash_formatter: JSON logs for logstash
2+
==========================================
3+

logstash_formatter/__init__.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import logging
2+
import socket
3+
import datetime
4+
import traceback as tb
5+
import json
6+
import sys
7+
8+
class LogstashFormatter(logging.Formatter):
9+
def __init__(self, source_host=None, defaults={}):
10+
11+
self.defaults = defaults
12+
if source_host:
13+
self.source_host = source_host
14+
else:
15+
try:
16+
self.source_host = socket.gethostname()
17+
except:
18+
self.source_host = ""
19+
20+
def format(self, record):
21+
22+
fields = record.__dict__
23+
24+
if isinstance(record.msg, dict):
25+
fields.update(record.msg)
26+
fields.pop('msg')
27+
msg = ""
28+
else:
29+
msg = record.getMessage()
30+
31+
if 'msg' in fields:
32+
fields.pop('msg')
33+
34+
if 'exc_info' in fields:
35+
if fields['exc_info']:
36+
formatted = tb.format_exception(*fields['exc_info'])
37+
fields['exception'] = formatted
38+
fields.pop('exc_info')
39+
40+
if 'exc_text' in fields and not fields['exc_text']:
41+
fields.pop('exc_text')
42+
43+
logr = self.defaults.copy()
44+
logr.update({'@message': msg,
45+
'@timestamp': datetime.datetime.now().isoformat(),
46+
'@source_host': self.source_host,
47+
'@fields': fields})
48+
49+
print(logr)
50+
return json.dumps(logr)

setup.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(name='logstash_formatter',
4+
version='0.5.0',
5+
description='JSON formatter meant for logstash',
6+
url='https://github.com/exoscale/python-logstash-formatter',
7+
author='Exoscale',
8+
author_email='[email protected]',
9+
license='MIT',
10+
packages=find_packages(),
11+
include_package_data=True,
12+
zip_safe=False)

0 commit comments

Comments
 (0)