rust-lang/log

logging without newline

efendioglukdz opened this issue · 9 comments

Hello folks,

is it possible that I put a logline without a implicit newline at the end of the line? the macro log::info!() puts always a new line at the end.
It would be nice if I had the possibility to distinguish it like the print! or println! macro it does!!!!

I don't think the info! macro adds a new line, the log implementation (that handles the actual logging) does that for you. You could change the implementation and simply not print a new line.

When I put
log::info!("Line1");
log::info!("Line2");
there is implicitely a newline between them. Mayb I dnt get it right, but where can I prevent this?

You are using some specific logging implementation that is deciding to put a newline between them. This crate doesn't have any control over that.

Well, I guess u mean flexi_logger.

ur right.
its decided in
.format_for_files(|w, now, record| { write!( w, "{};{: >7};{}", &format!("{}-{:02}-{:02} {:02}:{:02}:{:02}:{:03}",now.now().year(),now.now().month() as i32,now.now().day(),now.now().hour(),now.now().minute(),now.now().second(),now.now().millisecond()), &format!("[{}]",record.level()), &record.args() ) })
in the write! macro:
macro_rules! write { ($dst:expr, $($arg:tt)*) => { $dst.write_fmt($crate::format_args!($($arg)*)) }; }

I guess format_args! makes an newline

I guess format_args! makes an newline

No it doesn't. The flexi_logger implementation adds the new line.

I guess format_args! makes an newline

No, it doesn't and that's why rust introduces format_args_ln! which introduces a newline at the end.

Even if it's not baked into the log crate, it's still somewhat implied that each call to log! represents another log entry which most logger implementations will separate somehow. Be it a new line or another row in a data store or whatever. I still find the idea of this issue compelling. What if there was a macro a la log_but_wait_theres_more! and log_ok_entry_done!? ofc. those names are bullocks. They could be expanded to a check if logging is enabled and if so, prepare the log message in a String buffer or similar.

EDIT: looking through the crate code and familiarizing myself with std::fmt::Arguments and alike makes me realize it's not that straight forward. So while I'm no longer sure there's a technical solution to this, I'd still find it useful to have such functionality.

The concept of building up an event over an operation and emitting in a single blob at the end is sometimes called a "wide event". You can also do it using tracing, where multiple events are emitted within a single span.

It is an interesting concept, but not one that I think exists at the level of abstraction this crate is concerned with. An implementation of Log can decide whether or not a series of calls belong to the same entity or not.

I'll go ahead and close this one as the specific issue isn't related to log.