Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Rust Standard Library Cookbook

You're reading from   Rust Standard Library Cookbook Over 75 recipes to leverage the power of Rust

Arrow left icon
Product type Paperback
Published in Mar 2018
Publisher Packt
ISBN-13 9781788623926
Length 360 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Jan Hohenheim Jan Hohenheim
Author Profile Icon Jan Hohenheim
Jan Hohenheim
Daniel Durante Daniel Durante
Author Profile Icon Daniel Durante
Daniel Durante
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
1. Learning the Basics 2. Working with Collections FREE CHAPTER 3. Handling Files and the Filesystem 4. Serialization 5. Advanced Data Structures 6. Handling Errors 7. Parallelism and Rayon 8. Working with Futures 9. Networking 10. Using Experimental Nightly Features 1. Other Books You May Enjoy Index

Creating a custom logger


Sometimes you or your users might have very specific logging needs. In this recipe, we are going to learn how to create a custom logger to work with the log crate.

How to do it...

  1. Open the Cargo.toml file that was generated earlier for you.

  2. Under [dependencies], if you didn't do so in the last recipe, add the following line:

log = "0.4.1"

If you want, you can go to log's crates.io page (https://crates.io/crates/log) to check for the newest version and use that one instead.

  1. In the folderbin, create a file calledcustom_logger.rs.
  2. Add the following code and run it withRUST_LOG=custom_logger cargo run --bin custom_loggerif you're on a Unix-based system. Otherwise, run$env:RUST_LOG="custom_logger"; cargo run --bin custom_loggeron Windows:
1   #[macro_use]
2   extern crate log;
3 
4   use log::{Level, Metadata, Record};
5   use std::fs::{File, OpenOptions};
6   use std::io::{self, BufWriter, Write};
7   use std::{error, fmt, result};
8   use std::sync::RwLock;
9   use std::time...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images