Pushing Logs with a Logging Library - Rust (gelf_logger and log4rs-gelf)
Learn how to push your logs to Logs Data Platform using Rust.
Rust has a logging implementation (log) that 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.
- buffer the result into memory.
- batch send over a network using TCP/TLS.
- ensure fields follow the LDP naming conventions.
This tutorial shows how to push logs with two different libraries (gelf_logger and log4rs-gelf); use whichever you prefer.
Requirements
- Rust (we recommend the last stable version)
- an activated Logs Data Platform account
- at least one stream and its token
- install the serde crate with the derive feature
- install the log crate with serde feature
Method 1: 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: "bhs2.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.
Method 2: 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 log4rs.yaml. This file will be retrieved by the rust program to configure the framework.
Don't forget to modify the placeholder <YOUR-LDP-CLUSTER-ADDRESS> to the cluster where your stream resides. There is no need to put the Gelf port, e.g. "bhs2.logs.ovh.com".
Also, modify the placeholder <YOUR-WRITE-TOKEN> to the actual value of the write token of your stream.
Use this configuration in your project:
You could also look at the generated API documentation.
Go further
Browse all the Logs Data Platform guides.
Browse the rest of the OVHcloud US documentation for guides on our other products and services.
Learn more about Logs Data Platform on the OVHcloud US website.