1
- |PyPI_Status |
2
- |Build_Status |
3
- |Coverage_Status |
1
+ ######
2
+ graypy
3
+ ######
4
+
5
+ .. image :: https://img.shields.io/pypi/v/graypy.svg
6
+ :target: https://pypi.python.org/pypi/graypy
7
+ :alt: PyPI Status
8
+
9
+ .. image :: https://travis-ci.org/severb/graypy.svg?branch=master
10
+ :target: https://travis-ci.org/severb/graypy
11
+ :alt: Build Status
12
+
13
+ .. image :: https://readthedocs.org/projects/graypy/badge/?version=stable
14
+ :target: https://graypy.readthedocs.io/en/stable/?badge=stable
15
+ :alt: Documentation Status
16
+
17
+ .. image :: https://codecov.io/gh/severb/graypy/branch/master/graph/badge.svg
18
+ :target: https://codecov.io/gh/severb/graypy
19
+ :alt: Coverage Status
4
20
5
21
Description
6
22
===========
7
23
8
- Python logging handlers that send messages in the Graylog Extended
9
- Log Format (GELF _).
24
+ Python logging handlers that send log messages in the
25
+ Graylog Extended Log Format (GELF _).
26
+
27
+ graypy supports sending GELF logs to both Graylog2 and Graylog3 servers.
10
28
11
29
Installing
12
30
==========
13
31
14
32
Using pip
15
33
---------
16
34
17
- Install the basic graypy python logging handlers
35
+ Install the basic graypy python logging handlers:
18
36
19
- .. code-block :: bash
37
+ .. code-block :: console
20
38
21
39
pip install graypy
22
40
23
- Install with requirements for ``GELFRabbitHandler ``
41
+ Install with requirements for ``GELFRabbitHandler ``:
24
42
25
- .. code-block :: bash
43
+ .. code-block :: console
26
44
27
45
pip install graypy[amqp]
28
46
29
47
Using easy_install
30
48
------------------
31
49
32
- Install the basic graypy python logging handlers
50
+ Install the basic graypy python logging handlers:
33
51
34
- .. code-block :: bash
52
+ .. code-block :: console
35
53
36
- easy_install graypy
54
+ easy_install graypy
37
55
38
- Install with requirements for ``GELFRabbitHandler ``
56
+ Install with requirements for ``GELFRabbitHandler ``:
39
57
40
- .. code-block :: bash
58
+ .. code-block :: console
41
59
42
- easy_install graypy[amqp]
60
+ easy_install graypy[amqp]
43
61
44
62
Usage
45
63
=====
46
64
47
- Messages are sent to Graylog2 using a custom handler for the builtin logging
48
- library in GELF format
65
+ graypy sends GELF logs to a Graylog server via subclasses of the python
66
+ ` logging.Handler `_ class.
49
67
50
- .. code-block :: python
68
+ Below is the list of ready to run GELF logging handlers defined by graypy:
51
69
52
- import logging
53
- import graypy
54
-
55
- my_logger = logging.getLogger(' test_logger' )
56
- my_logger.setLevel(logging.DEBUG )
57
-
58
- handler = graypy.GELFUDPHandler(' localhost' , 12201 )
59
- my_logger.addHandler(handler)
70
+ * ``GELFUDPHandler `` - UDP log forwarding
71
+ * ``GELFTCPHandler `` - TCP log forwarding
72
+ * ``GELFTLSHandler `` - TCP log forwarding with TLS support
73
+ * ``GELFHTTPHandler `` - HTTP log forwarding
74
+ * ``GELFRabbitHandler `` - RabbitMQ log forwarding
60
75
61
- my_logger.debug(' Hello Graylog2.' )
76
+ UDP Logging
77
+ -----------
62
78
63
- Alternately, use ``GELFRabbitHandler `` to send messages to RabbitMQ and
64
- configure your Graylog2 server to consume messages via AMQP. This prevents
65
- log messages from being lost due to dropped UDP packets (``GELFUDPHandler ``
66
- sends messages to Graylog2 using UDP). You will need to configure RabbitMQ
67
- with a 'gelf_log' queue and bind it to the 'logging.gelf' exchange so
68
- messages are properly routed to a queue that can be consumed by
69
- Graylog2 (the queue and exchange names may be customized to your liking)
79
+ UDP Log forwarding to a locally hosted Graylog server can be easily done with
80
+ the ``GELFUDPHandler ``:
70
81
71
82
.. code-block :: python
72
83
@@ -76,12 +87,21 @@ Graylog2 (the queue and exchange names may be customized to your liking)
76
87
my_logger = logging.getLogger(' test_logger' )
77
88
my_logger.setLevel(logging.DEBUG )
78
89
79
- handler = graypy.GELFRabbitHandler( ' amqp://guest:guest@ localhost/ ' , exchange = ' logging.gelf ' )
90
+ handler = graypy.GELFUDPHandler( ' localhost' , 12201 )
80
91
my_logger.addHandler(handler)
81
92
82
- my_logger.debug(' Hello Graylog2 .' )
93
+ my_logger.debug(' Hello Graylog .' )
83
94
84
- Tracebacks are added as full messages
95
+ RabbitMQ Logging
96
+ ----------------
97
+
98
+ Alternately, use ``GELFRabbitHandler `` to send messages to RabbitMQ and
99
+ configure your Graylog server to consume messages via AMQP. This prevents log
100
+ messages from being lost due to dropped UDP packets (``GELFUDPHandler `` sends
101
+ messages to Graylog using UDP). You will need to configure RabbitMQ with a
102
+ ``gelf_log `` queue and bind it to the ``logging.gelf `` exchange so messages
103
+ are properly routed to a queue that can be consumed by Graylog (the queue and
104
+ exchange names may be customized to your liking).
85
105
86
106
.. code-block :: python
87
107
@@ -91,29 +111,22 @@ Tracebacks are added as full messages
91
111
my_logger = logging.getLogger(' test_logger' )
92
112
my_logger.setLevel(logging.DEBUG )
93
113
94
- handler = graypy.GELFUDPHandler( ' localhost' , 12201 )
114
+ handler = graypy.GELFRabbitHandler( ' amqp://guest:guest@ localhost/ ' , exchange = ' logging.gelf ' )
95
115
my_logger.addHandler(handler)
96
116
97
- try :
98
- puff_the_magic_dragon()
99
- except NameError :
100
- my_logger.debug(' No dragons here.' , exc_info = 1 )
117
+ my_logger.debug(' Hello Graylog.' )
101
118
102
-
103
- For more detailed usage information please see the documentation provided
104
- within graypy's handler's docstrings.
105
-
106
- Using with Django
107
- =================
119
+ Django Logging
120
+ --------------
108
121
109
122
It's easy to integrate ``graypy `` with Django's logging settings. Just add a
110
123
new handler in your ``settings.py ``:
111
124
112
125
.. code-block :: python
113
126
114
127
LOGGING = {
115
- ...
116
-
128
+ ' version ' : 1 ,
129
+ # other dictConfig keys here...
117
130
' handlers' : {
118
131
' graypy' : {
119
132
' level' : ' WARNING' ,
@@ -122,7 +135,6 @@ new handler in your ``settings.py``:
122
135
' port' : 12201 ,
123
136
},
124
137
},
125
-
126
138
' loggers' : {
127
139
' django.request' : {
128
140
' handlers' : [' graypy' ],
@@ -132,29 +144,57 @@ new handler in your ``settings.py``:
132
144
},
133
145
}
134
146
135
- Custom fields
136
- =============
147
+ Traceback Logging
148
+ -----------------
149
+
150
+ By default log captured exception tracebacks are added to the GELF log as
151
+ ``full_message `` fields:
152
+
153
+ .. code-block :: python
154
+
155
+ import logging
156
+ import graypy
157
+
158
+ my_logger = logging.getLogger(' test_logger' )
159
+ my_logger.setLevel(logging.DEBUG )
160
+
161
+ handler = graypy.GELFUDPHandler(' localhost' , 12201 )
162
+ my_logger.addHandler(handler)
163
+
164
+ try :
165
+ puff_the_magic_dragon()
166
+ except NameError :
167
+ my_logger.debug(' No dragons here.' , exc_info = 1 )
168
+
169
+ Default Logging Fields
170
+ ----------------------
171
+
172
+ By default a number of debugging logging fields are automatically added to the
173
+ GELF log if available:
137
174
138
- A number of custom fields are automatically added if available:
139
175
* function
140
176
* pid
141
177
* process_name
142
178
* thread_name
143
179
144
- You can disable these additional fields if you don't want them by adding
145
- an the ``debugging_fields=False `` to the handler:
180
+ You can disable automatically adding these debugging logging fields by
181
+ specifying ``debugging_fields=False `` in the handler's constructor :
146
182
147
183
.. code-block :: python
148
184
149
185
handler = graypy.GELFUDPHandler(' localhost' , 12201 , debugging_fields = False )
150
186
151
- graypy also supports additional fields to be included in the messages sent
152
- to Graylog2. This can be done by using Python's LoggerAdapter _ and
153
- Filter _. In general, LoggerAdapter makes it easy to add static information
154
- to your log messages and Filters give you more flexibility, for example to
155
- add additional information based on the message that is being logged.
187
+ Adding Custom Logging Fields
188
+ ----------------------------
189
+
190
+ graypy also supports including custom fields in the GELF logs sent to Graylog.
191
+ This can be done by using Python's LoggerAdapter _ and Filter _ classes.
192
+
193
+ Using LoggerAdapter
194
+ ^^^^^^^^^^^^^^^^^^^
156
195
157
- Example using LoggerAdapter _
196
+ LoggerAdapter _ makes it easy to add static information to your GELF log
197
+ messages:
158
198
159
199
.. code-block :: python
160
200
@@ -170,9 +210,13 @@ Example using LoggerAdapter_
170
210
my_adapter = logging.LoggerAdapter(logging.getLogger(' test_logger' ),
171
211
{' username' : ' John' })
172
212
173
- my_adapter.debug(' Hello Graylog2 from John.' )
213
+ my_adapter.debug(' Hello Graylog from John.' )
174
214
175
- Example using Filter _
215
+ Using Filter
216
+ ^^^^^^^^^^^^
217
+
218
+ Filter _ gives more flexibility and allows for dynamic information to be
219
+ added to your GELF logs:
176
220
177
221
.. code-block :: python
178
222
@@ -183,7 +227,7 @@ Example using Filter_
183
227
def __init__ (self ):
184
228
# In an actual use case would dynamically get this
185
229
# (e.g. from memcache)
186
- self .username = " John"
230
+ self .username = ' John'
187
231
188
232
def filter (self , record ):
189
233
record.username = self .username
@@ -197,25 +241,17 @@ Example using Filter_
197
241
198
242
my_logger.addFilter(UsernameFilter())
199
243
200
- my_logger.debug(' Hello Graylog2 from John.' )
244
+ my_logger.debug(' Hello Graylog from John.' )
201
245
202
- Contributors:
246
+ Contributors
247
+ ============
203
248
204
249
* Sever Banesiu
205
250
* Daniel Miller
206
251
* Tushar Makkar
207
252
* Nathan Klapstein
208
253
209
- .. _GELF : http://docs.graylog.org/en/latest/pages/gelf.html
210
- .. _LoggerAdapter : http://docs.python.org/howto/logging-cookbook.html#using-loggeradapters-to-impart-contextual-information
211
- .. _Filter : http://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information
212
-
213
- .. |Build_Status | image :: https://travis-ci.org/severb/graypy.svg?branch=master
214
- :target: https://travis-ci.org/severb/graypy
215
-
216
-
217
- .. |Coverage_Status | image :: https://codecov.io/gh/severb/graypy/branch/master/graph/badge.svg
218
- :target: https://codecov.io/gh/severb/graypy
219
-
220
- .. |PyPI_Status | image :: https://img.shields.io/pypi/v/graypy.svg
221
- :target: https://pypi.python.org/pypi/graypy
254
+ .. _GELF : https://docs.graylog.org/en/latest/pages/gelf.html
255
+ .. _logging.Handler : https://docs.python.org/3/library/logging.html#logging.Handler
256
+ .. _LoggerAdapter : https://docs.python.org/howto/logging-cookbook.html#using-loggeradapters-to-impart-contextual-information
257
+ .. _Filter : https://docs.python.org/howto/logging-cookbook.html#using-filters-to-impart-contextual-information
0 commit comments