Pushing logs with a logging library - Rust - gelf_logger and log4rs-gelf
Objective
This guide will explain how to push your logs to Logs Data Platform using Rust with two differents libraries. Use the one you prefer.
Rust has a logging implementation (log) which is widely used. OVHcloud has implemented this system to support the GELF format:
- gelf_logger: This is a minimal logger.
- log4rs-gelf: Based on gelf_logger, this implementation is compatible with the complex configurable framework log4rs.
Those loggers will:
- serialize log entries using the serde_gelf crate.
- bufferize the result into memory.
- batch send over network using TCP/TLS.
- ensure fields follow the LDP naming conventions.
Requirements
To complete this guide you will need:
- Rust. We recommend the last stable version.
- An activated Logs Data Platform account.
- To create at least one Stream and get its token.
- To install the serde crate with the derive feature.
- To install the log crate with the serde feature.
Instructions
First method: gelf_logger
You can install the gelf_logger crate by adding the dependency to your Cargo.toml:
Alternatively, the following cargo command will install it:
Here is a full main.rs file showing how to use the log and the gelf_logger API.
Don't forget to modify the placeholder to the cluster where your stream resides. There is no need to put the Gelf port. Example: "gra3.logs.ovh.com".
Don't forget to modify the placeholder to the actual value of the write token of your stream.
You could also look at the generated API documentaton.
Second method: log4rs-gelf
This method is an alternative to the previous one. Please consider the following as a different rust project. You need to be familiar with the log4rs framework
Install log4rs and log4rs-gelf in your Rust project.
Here is the modified Cargo.toml file:
Alternatively, use the following cargo commands:
Examples
From a YAML configuration file
Copy the content of this yaml file in a file log4rs.yaml. This file will be retrieved by the rust program to configure the framework.
Don't forget to replace the placeholder with the cluster where your stream resides. There is no need to put the Gelf port. Example: "gra3.logs.ovh.com".
Don't forget to replace the placeholder with the actual value of the write token of your stream
Replace the X-OVH-TOKEN value with your X-OVH-TOKEN stream value and the hostname with your cluster.
Use this configuration in your project:
You could also look at the generated API documentation.
Go further
- Getting Started: Quick Start
- Documentation: Guides
- Community hub: https://community.ovh.com
- Create an account: Try it!