aboutsummaryrefslogtreecommitdiff
path: root/examples/08_env_logging/src
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-10-09 17:51:07 -0700
committerZeyla Hellyer <[email protected]>2017-10-11 20:55:51 -0700
commit0df77b933ff5e98725252116069afad2dec9f89b (patch)
treea4c8c9f2a9056d10b58c6a19d22d13821a88a012 /examples/08_env_logging/src
parent Make `has_correct_permissions`, `has_correct_roles` and `has_all_requirement... (diff)
downloadserenity-0df77b933ff5e98725252116069afad2dec9f89b.tar.xz
serenity-0df77b933ff5e98725252116069afad2dec9f89b.zip
Add env_logger bot example
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);
+ }
+}