Skip to content

Instantly share code, notes, and snippets.

@a-recknagel
Last active November 23, 2020 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-recknagel/c96e9d70892053c2362b30d2bc55814f to your computer and use it in GitHub Desktop.
Save a-recknagel/c96e9d70892053c2362b30d2bc55814f to your computer and use it in GitHub Desktop.
log to a fluentd
import logging.config
workstation_IP = "?"
logging.config.dictConfig(
{
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"fluent_fmt": {
"()": "fluent.handler.FluentRecordFormatter",
"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"
},
},
"handlers": {
"fluent": {
"class": "fluent.handler.FluentHandler",
"host": workstation_IP,
"port": 9880,
"tag": "my_notebook",
"formatter": "fluent_fmt",
"level": "DEBUG",
},
},
"loggers": {
"": {
"handlers": ["fluent"],
"level": "DEBUG",
"propagate": True,
},
},
}
)
log = logging.getLogger(__name__)
log.info("Logger configuration set up successfully.")
# /tmp/fluentd.conf
<source>
@type forward
port 9880
bind 0.0.0.0
</source>
<match **>
@type stdout
</match>
@a-recknagel
Copy link
Author

a-recknagel commented Nov 23, 2020

pip-install fluent-logger and execute the logger code in the app that is creating the logs, and start a local fluentd server through docker by running docker run -p 9880:9880 -v path/to/test.conf:/fluentd/etc/test.conf -e FLUENTD_CONF=test.conf fluent/fluentd:latest.

Getting the correct workstation_IP might be tricky, as anything network-related usually is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment