libits/client/
logger.rs

1/*
2 * Software Name : libits-client
3 * SPDX-FileCopyrightText: Copyright (c) Orange SA
4 * SPDX-License-Identifier: MIT
5 *
6 * This software is distributed under the MIT license,
7 * see the "LICENSE.txt" file for more details or https://opensource.org/license/MIT/
8 *
9 * Authors: see CONTRIBUTORS.md
10 */
11
12use flexi_logger::Logger;
13use log::info;
14
15/// Creates a logger that outputs to stdout.
16///
17/// This function initializes a logger using the `flexi_logger` crate, which logs messages to the standard output (stdout).
18/// Logger's log level is set based on the environment variable or defaults to "info".
19///
20/// # Returns
21///
22/// A `Result` containing a `flexi_logger::LoggerHandle` if the logger is successfully initialized, or a boxed `dyn std::error::Error` if an error occurs.
23///
24/// # Errors
25///
26/// This function will return an error if the logger fails to initialize.
27///
28/// # Examples
29///
30/// ```rust
31/// use libits::client::logger::create_stdout_logger;
32/// let _logger = create_stdout_logger().expect("Logger initialization failed");
33/// ```
34///
35/// # Dependencies
36///
37/// This function requires the `flexi_logger` and `log` crates.
38///
39/// # Notes
40///
41/// Logger is configured to print messages to stdout and includes a message indicating that the logger is ready.
42///
43/// # See Also
44///
45/// - `flexi_logger::Logger`
46/// - `log::info`
47pub fn create_stdout_logger() -> Result<flexi_logger::LoggerHandle, Box<dyn std::error::Error>> {
48    let logger = Logger::try_with_env_or_str("info")?
49        .log_to_stdout()
50        .print_message()
51        .start()?;
52    info!("Logger ready on stdout");
53    Ok(logger)
54}