[][src]Module maidsafe_utilities::log

Allows initialising the env_logger with a standard message format. These functions can initialise logging for output to stdout only, or to a file and stdout. For more fine-grained control, create a file called log.toml in the root directory of the project, or in the same directory where the executable is. See log4rs docs for details about the format and structure of this file.

An example of a log message is:

WARN 19:33:49.245434200 <main> [example::my_mod main.rs:10] Warning level message.
^           ^             ^              ^         ^                  ^
|       timestamp         |           module       |               message
|                         |                        |
|                    thread name           file and line no.
|
level (ERROR, WARN, INFO, DEBUG, or TRACE)

Logging of the thread name is enabled or disabled via the show_thread_name parameter. If enabled, and the thread executing the log statement is unnamed, the thread name is shown as <unnamed>.

The functions can safely be called multiple times concurrently.

#Examples

#[macro_use]
extern crate log;
#[macro_use]
extern crate unwrap;
extern crate maidsafe_utilities;
use std::thread;

mod my_mod {
    pub fn show_warning() {
        warn!("A warning");
    }
}

fn main() {
    unwrap!(maidsafe_utilities::log::init(true));

    my_mod::show_warning();

    let unnamed = thread::spawn(move || info!("Message in unnamed thread"));
    let _ = unnamed.join();

    let _named = maidsafe_utilities::thread::named("Worker",
                                     move || error!("Message in named thread"));

    // WARN 16:10:44.989712300 <main> [example::my_mod main.rs:10] A warning
    // INFO 16:10:44.990716600 <unnamed> [example main.rs:19] Message in unnamed thread
    // ERROR 16:10:44.991221900 Worker [example main.rs:22] Message in named thread
}

Environment variable RUST_LOG can be set and fine-tuned to get various modules logging to different levels. E.g. RUST_LOG=mod0,mod1=debug,mod2,mod3 will have mod0 & mod1 logging at Debug and more severe levels while mod2 & mod3 logging at default (currently Warn) and more severe levels. RUST_LOG=trace,mod0=error,mod1 is going to change the default log level to Trace and more severe. Thus mod0 will log at Error level and mod1 at Trace and more severe ones.

Constants

MSG_TERMINATOR

Message terminator for streaming to Log Servers. Servers must look out for this sequence which demarcates the end of a particular log message.

Functions

init

Initialises the env_logger for output to stdout.

init_to_file

Initialises the env_logger for output to a file and optionally to the console asynchronously.

init_to_server

Initialises the env_logger for output to a server and optionally to the console asynchronously.

init_to_web_socket

Initialises the env_logger for output to a web socket and optionally to the console asynchronously. The log which goes to the web-socket will be both verbose and in JSON as filters should be present in web-servers to manipulate the output/view.

init_with_output_file

Initialises the env_logger for output to stdout and takes an output file name that will override the log configuration.

validate_web_socket_request

Check that a handshake request has the correct session ID value.