Relastic

Simple rust lib inspired by Serilog for application-wide logging:

Purpose

Log to Elastic from Rust applications

Intended consumers

Everyone. Do not use in production without first assessing the risks involved.

Main technologies

Available at

crates.io (relastic)

Requirements

More documentation

Testing locally

Known issues

Usage

```rs fn main() { let elasticconfig = match elasticconfig::ElasticConfig::new() { Ok(x) => x, Err(err) => panic!("Elastic not/improperly configured: {:?}", err), }; log::setupelasticlog( elastic_config, 100, );

/* ... */

log::flush();

} ```

Usage with Rocket.rs

Rocket by default does not allow blocking services to run in it's Tokio runtime. It does have a functionality for running blocking tasks as async, but the logging service should hold precendence over it; If rocket panics, the logger should be able to log this before the application terminates.

Here's how you can setup relastic with Rocket:

```rs fn main() { let elasticconfig = match elasticconfig::ElasticConfig::new() { Ok(x) => x, Err(err) => panic!("Elastic not/improperly configured: {:?}", err), }; log::setupelasticlog( elasticconfig, 100, ); // Create multi-threaded tokio runtime for rocket let rt = tokio::runtime::Runtime::new().unwrap(); rt.blockon(async { rocket_main().await; }); log::flush(); }

```

This also makes it so that we don't have to rely on Rocket-specific dependencies.

Log to console in dev-environment

You may want to simplify logging while you're developing, by logging to console instead:

```rs use intilitycloudlib::envtools::{getenv, getenvenum, Error}; use relastic::log::{self, ElasticConfig, LogEnvironment}; use std::collections::HashMap;

fn loadifprod(name: &str, envtype: &LogEnvironment) -> Result { match envtype { LogEnvironment::Development => Ok(String::withcapacity(0)), LogEnvironment::Production => getenv(name), } }

pub fn getconfig() -> Result { let envtype = getenvenum::("APPLICATIONENVIRONMENT")?; let username = loadifprod("ELASTICUSERNAME", &envtype)?; let password = loadifprod("ELASTICPASSWORD", &envtype)?; let url = loadifprod("ELASTICURL", &envtype)?; let environmentstring = getenvenum::("APPLICATIONENVIRONMENT")?; let applicationname = getenv("APPLICATIONNAME")?;

Ok(ElasticConfig {
    username,
    password,
    url,
    environment: environment_string,
    application_name,
})

}

pub fn getconfigorlogandpanic() -> ElasticConfig { match getconfig() { Ok(x) => x, Err(err) => { log::fatal( "Elastic config is missing or is misconfigured: {err}", HashMap::from([("err", format!("{}", err))]), ); panic!("{}", err) } } }

fn main() { let elasticconfig = getconfigorlogandpanic(); if elasticconfig.environment == LogEnvironment::Production { log::setupelasticlog(elasticconfig.clone(), 100); } else { log::setupconsolelog() }

/* ... */

log::flush();

}

```

This way you don't have to actually set any environment variables when working locally.

How to log

rs pub fn my_fn() { log::information( "Got the following message: {message}", HashMap::from([ ("message", "Hello World!".to_string()) ]) ); }

Contributing

You are welcome to reports bugs, contribute code or open issues.