Web-server and log to file

I'm creating a web-server that need log stuff on files and I struggle to found examples to do that the way I want.

Because I'm using tokio, hyper and routerify, I need a better solution than just opening the file every time I want to write something.

I would like to open the file, keep it somewhere and use a simple macro to write like the log crate can provides. But I don't understand where I should create my task, my object and where I should keep it.

Do you rustacean have conceptually answers to that?

1 Like

I'm pretty sure that tracing can do this.

Silly question, but why don't you just use the log crate then?

With logging libraries like log and tracing you'll normally initialize logging at the top of your main() function, then that will open the log file and set a global logger that writes to it (e.g. log::set_logger()).

If you are using the log crate then the fern logger lets you write to a file (see Output::file()), and the tracing-appender crate has utilities for doing rolling file logs.

1 Like

The https://crates.io/crates/tracing-log? I looked at some examples and didn't found something that can help me (yet).

  • fern seems to have the things I want, I will look at it, thank you.
  • tracing-appender the name didn't ring a bell when I saw it the 1st time. I will also look at it.
2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.