Geal / rust-syslog

Send syslog messages from Rust
MIT License
109 stars 55 forks source link

Support Log 0.4 #32

Closed mssun closed 6 years ago

mssun commented 6 years ago

The Log crate (https://crates.io/crates/log) is 0.4.1 now. However rust-syslog does not support the newer version. I found that are some naming changes for 0.4. (e.g., LogLevel -> Log). Therefore I cannot use syslog follow the instruction from the Rust Cook Book: https://rust-lang-nursery.github.io/rust-cookbook/logging.html#ex-log-syslog

daboross commented 6 years ago

It's worth noting that even without official support from syslog, its possible to shim support into a log-0.4-based logging library. This is what fern does while we wait for an update to the crate.

mssun commented 6 years ago

@daboross Thanks. I tried to get this issue done by myself. It does not need too many modifications. However, I met an issue with chain_err. I'm not familiar with it. I don't know if I correctly modify the code to support log 0.4.

Here is an example:

@@ -272,15 +276,14 @@ pub fn init_unix_custom<P: AsRef<Path>>(facility: Facility, log_level: log::LogL
     pid:      pid,
   };
   unix_custom(formatter, path).and_then(|logger| {
-    log::set_logger(|max_level| {
-      max_level.set(log_level);
-      Box::new(BasicLogger::new(logger))
-    }).chain_err(|| ErrorKind::Initialization)
+    log::set_logger(&BasicLogger::new(logger)).map_err(|_| ErrorKind::Initialization)?;
+    log::set_max_level(log_level);
+    Ok(())
   })
 }

I don't know how to handle this one (if I change orders of the two lines), which will give an error:

@@ -255,15 +261,13 @@ pub fn init_unix(facility: Facility, log_level: log::LogLevelFilter) -> Result<(
     pid:      pid,
   };
   unix(formatter).and_then(|logger| {
-    log::set_logger(|max_level| {
-      max_level.set(log_level);
-      Box::new(BasicLogger::new(logger))
-    }).chain_err(|| ErrorKind::Initialization)
+    log::set_max_level(log_level);
+    log::set_logger(&BasicLogger::new(logger)).chain_err(|_| ErrorKind::Initialization)
   })
 }
daboross commented 6 years ago

Hm- I'm not too familiar with error-chain either, but I think something like this should work:

@@ -272,15 +276,14 @@ pub fn init_unix_custom<P: AsRef<Path>>(facility: Facility, log_level: log::LogL
     pid:      pid,
   };
   unix_custom(formatter, path).and_then(|logger| {
-    log::set_logger(|max_level| {
-      max_level.set(log_level);
-      Box::new(BasicLogger::new(logger))
-    }).chain_err(|| ErrorKind::Initialization)
+    log::set_boxed_logger(Box::new(BasicLogger::new(logger))).chain_err(|| ErrorKind::Initialization)?
+
+    log::set_max_level(log_level);
+
+    Ok(())
   })
 }

Is the source of your error trying to use log::set_logger, possibly? log 0.4 changed what used to be set_logger into the set_boxed_logger function. set_logger is really only useful in static situations now.

mssun commented 6 years ago

I don't know the difference oflog::set_logger and log::set_boxed_logger, but I can compile with log::set_logger function. Anyway, I think it's easy to support 0.4 right now.

Geal commented 6 years ago

syslog now uses log 0.4