aboutsummaryrefslogtreecommitdiff
path: root/examples/08_env_logging/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/08_env_logging/src')
-rw-r--r--examples/08_env_logging/src/main.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/examples/08_env_logging/src/main.rs b/examples/08_env_logging/src/main.rs
new file mode 100644
index 0000000..db34037
--- /dev/null
+++ b/examples/08_env_logging/src/main.rs
@@ -0,0 +1,45 @@
+#[macro_use] extern crate log;
+
+extern crate env_logger;
+extern crate serenity;
+
+use serenity::prelude::*;
+use serenity::model::event::ResumedEvent;
+use serenity::model::Ready;
+use std::env;
+
+struct Handler;
+
+impl EventHandler for Handler {
+ fn on_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 on_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);
+
+ if let Err(why) = client.start() {
+ error!("Client error: {:?}", why);
+ }
+}