aboutsummaryrefslogtreecommitdiff
path: root/examples/08_env_logging/src/main.rs
blob: 8e2f664b70a9716366d6ba8de31bd8623bc197db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#[macro_use] extern crate log;

extern crate env_logger;
extern crate serenity;

use std::env;

use serenity::{
    model::{event::ResumedEvent, gateway::Ready},
    prelude::*,
};

struct Handler;

impl EventHandler for Handler {
    fn ready(&self, _: Context, ready: Ready) {
        // Log at the INFO level. This is a macro from the `log` crate.
        info!("{} is connected!", ready.user.name);
    }

    fn resume(&self, _: Context, resume: ResumedEvent) {
        // Log at the DEBUG level.
        //
        // In this example, this will not show up in the logs because DEBUG is
        // below INFO, which is the set debug level.
        debug!("Resumed; trace: {:?}", resume.trace);
    }
}

fn main() {
    // Call env_logger's initialize function, which configures `log` via
    // environment variables.
    //
    // For example, you can say to log all levels INFO and up via setting the
    // environment variable `RUST_LOG` to `INFO`.
    env_logger::init().expect("Unable to init env_logger");

    // Configure the client with your Discord bot token in the environment.
    let token = env::var("DISCORD_TOKEN")
        .expect("Expected a token in the environment");

    let mut client = Client::new(&token, Handler).expect("Err creating client");

    if let Err(why) = client.start() {
        error!("Client error: {:?}", why);
    }
}