Browse Source

Initial commit

Sam Clements 10 years ago
commit
87edf7228d
5 changed files with 88 additions and 0 deletions
  1. 2 0
      .gitignore
  2. 8 0
      Cargo.toml
  3. 42 0
      README.md
  4. 9 0
      examples/logging_example.rs
  5. 27 0
      src/lib.rs

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+target
+Cargo.lock

+ 8 - 0
Cargo.toml

@@ -0,0 +1,8 @@
+[package]
+name = "simple_logger"
+version = "0.0.1"
+authors = ["Sam Clements <[email protected]>"]
+
+[dependencies]
+log = "~0.2.4"
+time = "~0.1.17"

+ 42 - 0
README.md

@@ -0,0 +1,42 @@
+# simple_logger [![](https://img.shields.io/github/tag/borntyping/rust-simple_logger.svg)](https://github.com/borntyping/rust-simple_logger/tags) [![](https://img.shields.io/travis/borntyping/rust-simple_logger.svg)](https://travis-ci.org/borntyping/rust-simple_logger) [![](https://img.shields.io/github/issues/borntyping/rust-simple_logger.svg)](https://github.com/borntyping/rust-simple_logger/issues)
+
+A logger that prints all messages and uses an output format similar to [Supervisord](http://supervisord.org/).
+
+Future updates may include coulored output based on the log level of the message and selecting a `max_log_level` based on the value of an input string (e.g. from a flag parsed by [docopt](https://github.com/docopt/docopt.rs)).
+
+Usage
+-----
+
+```rust
+#[macro_use]
+extern crate log;
+extern crate simple_logger;
+
+fn main() {
+    simple_logger::init();
+
+    warn!("This is an example message.");
+}
+```
+
+This outputs:
+
+```
+2015-02-24 01:05:20 WARN [logging_example] This is an example message.
+```
+
+You can run the above example with:
+
+```bash
+cargo run --example logging_example
+```
+
+Licence
+-------
+
+`simple_logger` is licenced under the [MIT Licence](http://opensource.org/licenses/MIT).
+
+Authors
+-------
+
+Written by [Sam Clements]([email protected]).

+ 9 - 0
examples/logging_example.rs

@@ -0,0 +1,9 @@
+#[macro_use]
+extern crate log;
+extern crate simple_logger;
+
+fn main() {
+    simple_logger::init();
+
+    warn!("This is an example message.");
+}

+ 27 - 0
src/lib.rs

@@ -0,0 +1,27 @@
+extern crate log;
+extern crate time;
+
+struct SimpleLogger;
+
+impl log::Log for SimpleLogger {
+    fn enabled(&self, level: log::LogLevel, _module: &str) -> bool {
+        level <= log::LogLevel::Trace
+    }
+
+    fn log(&self, record: &log::LogRecord) {
+        println!(
+            "{} {} [{}] {}",
+            time::strftime("%Y-%m-%d %H:%M:%S", &time::now()).unwrap(),
+            record.level(),
+            record.location().module_path,
+            record.args());
+    }
+}
+
+/// Initializes the global logger with a SimpleLogger instance
+pub fn init() {
+    log::set_logger(|max_log_level| {
+        max_log_level.set(log::LogLevelFilter::Trace);
+        return Box::new(SimpleLogger);
+    }).unwrap();
+}