-
Notifications
You must be signed in to change notification settings - Fork 1
/
graylogger.py
executable file
·179 lines (144 loc) · 6.03 KB
/
graylogger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/python
# -*- coding: utf-8 -*.
import sys
import os
import logging
import argparse
import graypy
import json
def bailout(msg):
sys.stderr.write(msg + '\n')
exit(1)
def check_args():
loglevels = { 'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR,
'CRITICAL': logging.CRITICAL }
cmdline_options = { "facility": "PythonLogger",
"version": "1.0",
"level": 1,
"nolog": False,
"template": None,
"message": None,
"server": None,
"data": None,
"port": 12201 }
parser = argparse.ArgumentParser(description='send a message to a graylog server',
usage='%(prog)s [options] SERVER MESSAGE')
parser.add_argument("server", help="graylog server name")
parser.add_argument("message", help="message being sent or @FILENAME to read from FILENAME or - to read from StdIn")
parser.add_argument("-l", "--level", help="log level (defaults to ALERT)", choices=loglevels)
parser.add_argument("-f", "--facility", help="facility name (defaults to 'PythonLogger')")
parser.add_argument("-p", "--port", help="graylog port (defaults to 12201)", type=int)
parser.add_argument("-d", "--data", help="additional data field (key:value)", action="append")
parser.add_argument("-t", "--template", help="specify template to parse log message", type=str)
parser.add_argument("-n", "--nolog", help="do not log, simulate and show on console", action="store_true")
parser.add_argument("--file", help="file name to be logged", type=str, default=os.path.basename(sys.argv[0]))
parser.add_argument("--logfile", help="app log file name to be logged", type=str, default="")
args = parser.parse_args()
cmdline_options['server'] = args.server
# process message, may be a string, a file ref starting with '@'
# or a dash to read from stdin
if args.message.startswith('@'):
fname = args.message[1:]
if os.path.exists(fname):
cmdline_options['message'] = open(fname).read()
else:
bailout('file {0} not found'.format(fname))
else:
if args.message.strip() == '-':
_fullmsg = ""
for line in iter(sys.stdin.readline, ""):
_fullmsg += line + ' '
cmdline_options['message'] = _fullmsg
else:
cmdline_options['message'] = args.message
# check port override
if args.port is not None:
cmdline_options['port'] = args.port
# process facility option
if args.facility is not None:
cmdline_options['facility'] = args.facility
# process template option
if args.template is not None:
cmdline_options['template'] = args.template
# process simulation option
cmdline_options['nolog'] = args.nolog
# set the level
if args.level is not None:
cmdline_options['level'] = loglevels[args.level.upper()]
# process additional data fields, if present
cmdline_options['data'] = {}
if args.data is not None:
for entry in args.data:
entry = entry.strip().split(':')
key = entry[0]
value = ''.join(entry[1:])
cmdline_options['data'][key] = value
cmdline_options['appfilename'] = args.file
cmdline_options['logfilename'] = args.logfile
return cmdline_options
def parse_log_string(format_description, input_string):
DELIMITERS = {'open': ['"', '[', '(', '|'], 'close': ['"', ']', ')', '|']}
identifier_list = format_description.strip().split(" ")
wo = 0
result = {}
for identifier in identifier_list:
open_delim = None
close_delim = None
if identifier[0:1] in DELIMITERS['open']:
open_delim = identifier[0:1]
identifier = identifier[1:]
if identifier[-1] in DELIMITERS['close']:
close_delim = identifier[-1]
identifier = identifier[0:-1]
while input_string[wo:wo + 1] in [' ', '\t']:
wo += 1
token = ''
if open_delim is not None:
while input_string[wo:wo + 1] != open_delim and input_string[wo:wo + 1] != '':
wo += 1
token = open_delim
wo += 1
if close_delim is not None:
while input_string[wo:wo + 1] != close_delim and input_string[wo:wo + 1] != '':
token += input_string[wo:wo + 1]
wo += 1
token += close_delim
wo += 1
else:
while input_string[wo:wo + 1] not in [' ', '\t'] and input_string[wo:wo + 1] != '':
token += input_string[wo:wo + 1]
wo += 1
result[identifier] = token
return result
def filter(log_record : logging.LogRecord) -> bool:
log_record.logfile = args['logfilename']
log_record.file = args['appfilename']
return True
args = check_args()
def main():
try:
my_logger = logging.getLogger(args['facility'])
my_logger.setLevel(args['level'])
handler = graypy.GELFUDPHandler(args['server'], args['port'], debugging_fields=False)
my_logger.addHandler(handler)
my_logger.addFilter(filter)
d = args['data']
if args['template'] is not None:
parsing = parse_log_string(args['template'], args['message'])
d.update(parsing)
if args['nolog'] is True:
print('Simulation mode:')
print('Log level: {0}'.format(args['level']))
print('Facility: {0}'.format(args['facility']))
print('Server: {0}'.format(args['server']))
print('Message: {0}'.format(args['message']))
print('Custom fields: {0}'.format(json.dumps(d)))
else:
my_logger.log(args['level'], args['message'], extra=d)
except Exception as e:
bailout("Exception during log operation: {0}".format(e))
if __name__ == '__main__':
main()