krossbar_log_lib/lib.rs
1//! Krossbar logging library
2//!
3//! The library is used to connect to Krossbar logging service to send log messages
4//! and receive log control commands.
5//!
6//! See [Krossbar log control](https://crates.io/crates/krossbar-log-control) documentation
7//! on how to control logging.
8//!
9//! The library uses Unix stream connection to send logging messages, which means you need
10//! running [Krossbar logger](https://crates.io/crates/krossbar-logger) to log mesage.
11//! In case service can't connect to the logger, it logs to stdout.
12//!
13//! Also, you can use [Logger] manually to control whether log into stdout or send
14//! message to the logger. Both option are independent.
15//!
16//! In case you use Krossbar logger, you have to run logging loop using [Logger::run].
17//!
18//! # Examples
19//! ```rust
20//! use std::time::Duration;
21//!
22//! use log::*;
23//! use tokio::select;
24//!
25//! use krossbar_log_lib::init_logger;
26//!
27//! async fn log_example() {
28//! let logger = init_logger("com.examples.logging", LevelFilter::Trace, true).await;
29//!
30//! tokio::spawn(logger.run());
31//!
32//! loop {
33//! error!("Error message");
34//! warn!("Warning message");
35//! info!("Info message");
36//! debug!("Debug message");
37//! trace!("Trace message");
38//!
39//! select! {
40//! _ = tokio::time::sleep(Duration::from_secs(1)) => {},
41//! _ = tokio::signal::ctrl_c() => { return; }
42//! }
43//! }
44//! }
45//! ```
46pub mod logger;
47mod rpc;
48
49use log::LevelFilter;
50
51use krossbar_log_common::DEFAULT_LOGGER_SOCKET_PATH;
52
53pub use logger::Logger;
54
55/// Init logger.
56/// **service_name** is a client service name. It must be uniques across the system.
57/// **log_to_stdout** sets if logger should log to stdout. If set, library
58/// logs to stdout even if it then sends messages to the logger.
59pub async fn init_logger(service_name: &str, level: LevelFilter, log_to_stdout: bool) -> Logger {
60 Logger::new(
61 service_name,
62 level,
63 log_to_stdout,
64 Some(DEFAULT_LOGGER_SOCKET_PATH.into()),
65 )
66 .await
67 .unwrap()
68}