@@ -74,15 +74,85 @@ This client-library also has `FluentHandler` class for Python logging module.
74
74
import logging
75
75
from fluent import handler
76
76
77
+ custom_format = {
78
+ ' host' : ' %(hostname)s ' ,
79
+ ' where' : ' %(module)s .%(funcName)s '
80
+ ' type' : ' %(levelname)s ' ,
81
+ ' stack_trace' : ' %(exc_text)s '
82
+ }
83
+
77
84
logging.basicConfig(level = logging.INFO )
78
85
l = logging.getLogger(' fluent.test' )
79
86
h = handler.FluentHandler(' app.follow' , host = ' host' , port = 24224 )
80
- h.setFormatter(handler.FluentRecordFormatter())
87
+ formatter = handler.FluentRecordFormatter(custom_format)
88
+ h.setFormatter(formatter)
81
89
l.addHandler(h)
82
90
l.info({
83
91
' from' : ' userA' ,
84
92
' to' : ' userB'
85
93
})
94
+ l.info(' {"from": "userC", "to": "userD"}' )
95
+ l.info(" This log entry will be logged with the additional key: 'message'." )
96
+ ```
97
+
98
+ You can also customize formatter via logging.config.dictConfig
99
+
100
+ ``` python
101
+ import logging.config
102
+ import yaml
103
+
104
+ with open (' logging.yaml' ) as fd:
105
+ conf = yaml.load(fd)
106
+
107
+ logging.config.dictConfig(conf[' logging' ])
108
+ ```
109
+
110
+ A sample configuration ` logging.yaml ` would be:
111
+
112
+ ``` python
113
+ logging:
114
+ version: 1
115
+
116
+ formatters:
117
+ brief:
118
+ format : ' %(message)s '
119
+ default:
120
+ format : ' %(asctime)s %(levelname)-8s %(name)-15s %(message)s '
121
+ datefmt: ' %Y-%m-%d %H:%M:%S'
122
+ fluent_fmt:
123
+ ' ()' : fluent.handler.FluentRecordFormatter
124
+ format :
125
+ level: ' %(levelname)s '
126
+ hostname: ' %(hostname)s '
127
+ where: ' %(module)s .%(funcName)s '
128
+
129
+ handlers:
130
+ console:
131
+ class : logging.StreamHandler
132
+ level: DEBUG
133
+ formatter: default
134
+ stream: ext:// sys.stdout
135
+ fluent:
136
+ class : fluent.handler.FluentHandler
137
+ host: localhost
138
+ port: 24224
139
+ tag: test.logging
140
+ formatter: fluent_fmt
141
+ level: DEBUG
142
+ null:
143
+ class : logging.NullHandler
144
+
145
+ loggers:
146
+ amqp:
147
+ handlers: [null]
148
+ propagate: False
149
+ conf:
150
+ handlers: [null]
151
+ propagate: False
152
+ ' ' : # root logger
153
+ handlers: [console, fluent]
154
+ level: DEBUG
155
+ propagate: False
86
156
```
87
157
88
158
## Testing
0 commit comments