The Graylog Extended Log Format (GELF) is a structured log format representation which can be sent over network as a JSON string.
Add in your Cargo.toml
:
toml
[dependencies]
serde-value = "0.5"
serde_derive = "1.0"
serde_gelf = "0.1"
serde_json = "1.0"
Create a structure which implement the Serialize
trait:
```rust
extern crate serdederive; extern crate serdegelf; extern crate serdejson; extern crate serdevalue;
use std::collections::BTreeMap;
struct SubFoo { sa: String, sb: isize, }
struct Foo {
a: u32,
b: String,
c: Vec
fn main() {
let foo = Foo {
a: 15,
b: "hello".into(),
c: vec![true, false],
d: {
let mut map = BTreeMap::new();
map.insert("k1".tostring(), serdevalue::Value::F64(5.9));
map.insert("k2".tostring(), serdevalue::Value::Bool(false));
map
},
e: SubFoo { sa: "test".tostring(), sb: 5 },
};
println!("{}", serdejson::tostringpretty(& serdegelf::toflatdict(&foo).unwrap()).unwrap());
}
**Output**:
json
{
"a": 15,
"b": "hello",
"c0": true,
"c1": false,
"dk1": 5.9,
"dk2": false,
"esa": "test",
"e_sb": 5
}
```
To send special type like number or boolean, LDP uses suffixes as naming convention to force ES type:
| Suffix | ES Type | About | |-------------------|----------|------------------------------------------------------------------------------------------------------------------------------------| | _double | double | Unsigned number | | _float | double | Floating value in double in java representation : double-precision 64-bit IEEE 754 floating point | | _long | long | 64 bit signed long type,which has a minimum value of -263 and a maximum value of 263-1 | | _bool | boolean | Expected values: "true" or "false".WARNING : GELF does not support boolean types you will have to send "true" or "false" in String | | Everything else | String | Anything else will be considered a string |
To enable suffixes, update Cargo.toml
and set the ovh-ldp feature:
```toml
serde_gelf = { version = "0.1", features = ["ovh-ldp"] }
[dependencies.serdegelf]
version = "0.1"
features = ["ovh-ldp"]
Now the output of the previous example will be:
json
{
"adouble": 15,
"b": "hello",
"c0bool": true,
"c1bool": false,
"dk1float": 5.9,
"dk2bool": false,
"esa": "test",
"esb_long": 5
}
```
This library provides a macro gelf_record!
to create a gelf record according
to the GELF Payload Specification.
To enable macros, just activate the macros on crate import:
```rust
extern crate serdegelf; extern crate serdejson;
fn main() {
let rec = gelfrecord!("hello");
println!("{}", serdejson::tostringpretty(&rec).unwrap());
}
**Output**:
json
{
"facility": "src",
"file": "examples/src/main.rs",
"host": "myhostname",
"level": 1,
"levelname": "Alert",
"line": 11,
"shortmessage": "hello",
"timestamp": 1554980878.241851,
"version": "1.1"
}
```
Licensed under BSD 3-Clause License or (https://opensource.org/licenses/BSD-3-Clause)