8000 Merge pull request #39 from ngiallelis/update-readme-handler-formatter · fluent/fluent-logger-python@046d0a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 046d0a0

Browse files
committed
Merge pull request #39 from ngiallelis/update-readme-handler-formatter
Update Readme, including custom formatter configuration for handler
2 parents b50bb3a + 967b652 commit 046d0a0

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,85 @@ This client-library also has `FluentHandler` class for Python logging module.
7474
import logging
7575
from fluent import handler
7676

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+
7784
logging.basicConfig(level=logging.INFO)
7885
l = logging.getLogger('fluent.test')
7986
h = handler.FluentHandler('app.follow', host='host', port=24224)
80-
h.setFormatter(handler.FluentRecordFormatter())
87+
formatter = handler.FluentRecordFormatter(custom_format)
88+
h.setFormatter(formatter)
8189
l.addHandler(h)
8290
l.info({
8391
'from': 'userA',
8492
'to': 'userB'
8593
})
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
86156
```
87157

88158
## Testing

0 commit comments

Comments
 (0)
0