estk/log4rs

log4rs is a highly configurable logging framework modeled after Java's Logback

If you are using the file rotation in your configuration there is a known substantial performance issue so listen up!

log4rs

log4rs is a highly configurable logging framework modeled after Java's Logback and log4j libraries.

Warning

If you are using the file rotation in your configuration there is a known substantial performance issue so listen up! By default the gzip feature is enabled and when rolling files it will zip log archives automatically. This is a problem when the log archives are large as the zip happens in the main thread and will halt the process while the zip is completed. Be advised that the gzip feature will be removed from default features as of 1.0.

The methods to mitigate this are as follows.

  1. Use the background_rotation feature which spawns an os thread to do the compression.
  2. Disable the gzip feature with --no-default-features.
  3. Ensure the archives are small enough that the compression time is acceptable.

For more information see the PR that added background_rotation.

Quick Start

log4rs.yaml:

lib.rs:

Rust Version Requirements

1.46

Building for Dev

  • Run the tests: cargo test --all-features
  • Run the tests for windows with cross: cross test --target x86_64-pc-windows-gn
  • Run the tests for all individual features: ./test.sh
  • Run the tests for all individual features for windows with cross: ./test.sh win

License

Licensed under either of

  • Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
  • MIT license (LICENSE-MIT or #404)

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you shall be dual licensed as above, without any additional terms or conditions.

Issues

Collection of the latest Issues

jornfranke

jornfranke

Comment Icon0

Hi,

I would like to use log4rs and compile it to the target wasm32-wasi:

Output:

This does not work as there is an issue with the thread-id library: https://github.com/ruuda/thread-id/issues/10 Note: The issue is rather old and wasm allows compilation with threads (no other library causes problems...). If I remove log4rs as a dependency then I can compile.

Do you have a pointer where thread-id is used and if this could be maybe removed from log4rs?

I think making it work on WASM would extend the potential use cases of log4rs significantly.

Thank you.

madelaney

madelaney

Comment Icon1

I was wondering if it was possible, when using a YAML configuration, to have the date/time inserted as the file name when configuring a file appender?

here is an example, where {d} would be a place holder for the datetime:

/shrug

  • Mike D.
ivanschuetz

ivanschuetz

Comment Icon0

Is it possible to check if the logger is already initialized - to avoid having to deal with "attempted to set a logger after the logging system was already initialized" (which since it's now an anyhow::Result would have to be matched via text)?

I'd like to enable logging for integration tests - don't have anything at the moment that runs before all the tests and would prefer to not have to use external crates or macros just for the logging.

DarthHater

DarthHater

blocked
Comment Icon4

Hi there!

The crate time before 0.2.23 has a vulnerability, more info here: https://github.com/RustSec/advisory-db/blob/main/crates/time/RUSTSEC-2020-0071.md

FYI time is brought in via chrono to this project.

I've made a comment on a chrono issue where they are discussing upgrading time: https://github.com/chronotope/chrono/issues/553#issuecomment-883630753

I imagine you can't do too much until chrono updates, but pointing this out seemed like a good idea!

Cheers, Jeffry

cc @bhamail

madelaney

madelaney

question
Comment Icon2

All,

Is it possible to have the log4rs config to be yaml but to use include_str! at compile time to "bake" in the default configuration using yaml?

I tried to look at log4rs::init_file but those seem like they need the file at runtime but I want to bake in a default configuration so that users don't have to have a yaml file too.

  • Mike D.
OvermindDL1

OvermindDL1

enhancement
Comment Icon3

Would it be possible to add an option to RollingFileAppender to call the roller upon initial load before any logging is done? I attempted to make an OnLaunchTrigger but that lets one log message leak through so I end up getting a file with that single log, or I get that single new log entry at the end of the old log file. Optimally I'm looking to roll the logs on every load of the application based on the configuration of the roller before any logs are logged (of course the logger is initialized before any logging is done).

CHCP

CHCP

enhancement
Comment Icon2

Hi there, ​ ​I found when a rolling happened, all rolling log files will be renamed, for example:

If there are 100 or more rolling files, it will take some time to rename files, and will effort the main process. Would you like to consider a better way to rename rolling files:

1、record the current rolling file number in a new file, such as 2; 2、when rolling happened, the ohlc.log rename to ohlc.log.2.gz, only rename one file, don't need to rename all rolling files.

Thank you very much.

Coding-Badly

Coding-Badly

bug
Comment Icon9

Background

FixedWindowRoller can optionally use a thread to roll the log files. If that thread is active when the program terminates the most recent log file can be orphaned. There are very likely other possible symptoms like leaving a gap in the sequence.

Example

An example is available here. Fetch it. Run it. There should be a new directory named tmp in the crate. In that directory will be a single orphaned log file. The problem was been reproduced under Linux and Windows.

Coding-Badly

Coding-Badly

enhancement
Comment Icon7

Linux and Windows allow troublesome filenames which are not tolerated in Rust Strings. Some information is available here. Rust has OsStr and OsString to help deal with the problem.

FixedWindowRollerBuilder::build takes a &str for the filename pattern. That makes it impossible to place the log archives in a directory that has a troublesome name.

0xpr03

0xpr03

Comment Icon6

Currently init_file does use anyhow which does not implement std::error::Error. Thus it is impossible to directly convert from this type or use ? on it when using anything else than anyhow.

In my case this is stable_eyre for backtraces. the trait std::error::Erroris not implemented foranyhow::Error``

I'd suggest using thiserror which as stated per docs is more suited to libs versus applications.

See also https://github.com/yaahc/eyre/issues/31 and https://github.com/dtolnay/anyhow/pull/28

im-konge

im-konge

enhancement
Comment Icon4

In my simple yaml, I tried to add colorization for the pattern:

or

both of these are not allowed. I saw that this enhancement was added in #5 . Maybe after that 5 years the code is removed and I'm not able to add it to the pattern.

Is there any possibility that the colored output will be added to log4rs? Or is there any way how to do it that I didn't find yet? If yes, can you please add some mention to the documentation so it will be clear for everyone?

Thanks

pm100

pm100

enhancement
Comment Icon3

I have an project with a very noisy log. So I have added dedup facility to log4rs. ie you get this

Would you like the code for this. At the moment I only added it to the file appender. I am not 100% happy with how it fits into the code, there is a common helper that has to be added to each appender (I tried as a filter but no go)

SarthakSingh31

SarthakSingh31

enhancement
Comment Icon3

It would be handy if the config builder could consume already created configs and config builders.

It will allow frameworks to consume a user created configuration in their initialization and then append their needs to it before initializing log4rs.

What it would look like:

Versions

Find the latest versions by id

v0.12.0 - May 06, 2020

New

  • Derived Clone for Handle.

Changed

Fixed

  • Build warnings
  • Docs typo

v0.11.0 - Mar 11, 2020

A performance issue was discovered with gzip and rolling logs, the background_rotation feature was added to mitigate this by spawning a background thread to perform the rotation in. Shout out to @yakov-bakhmatov for the PR!

New

  • background_rotation feature which rotates and compresses log archives in a background thread.

Changed

  • Deprecate xml feature in preparation for removal.
  • Simplify and increase visibility of docs.
  • Swap some synchronization primitives to use parking_lot implementations.

v0.10.0 - Jan 31, 2020

This is a big release as we're moving to rust 2018 edition!

New

  • More badges in the readme.

Changed

  • Use rust 2018 edition.
  • Minimum rust version is 1.38.0
  • Update arcswap, serde-value and serde-xml-rs.

Fixed

  • Deprecate len method on rolling_file.
  • Windows build issue after 2018 edition.

v0.8.3 - Apr 03, 2019

v0.8.2 - Apr 03, 2019

v0.8.1 - Oct 17, 2018

v0.8.0 - Dec 26, 2017

v0.7.0 - Apr 26, 2017

  • Update to serde 1.0

v0.6.3 - Apr 06, 2017

  • Fix console appender to actually log to stdout when requested.

v0.6.2 - Mar 01, 2017

  • Fix handling of non-0 bases in rolling file appender.

v0.6.1 - Feb 11, 2017

  • Add TOML support back in.

v0.6.0 - Feb 10, 2017

  • Update to serde 0.9
  • Use serde_derive instead of manual codegen
  • Enable most features by default. This increases compile times a bit, but is way less confusing for people since components aren't randomly missing.
  • Drop TOML support. The toml crate hasn't yet been released with support for serde 0.9, but we'll add support back when that lands.
  • Restructure config deserialization. A log4rs config can now be embedded in other config structures and deserialized by downstream users.

v0.5.2 - Nov 26, 2016

  • Make Deserializers Clone.

v0.5.1 - Nov 21, 2016

  • Update serde_yaml
  • Fix file modification time checks in config reloader

Information - Updated Apr 03, 2022

Stars: 634
Forks: 93
Issues: 37

Repositories & Extras

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically

Rust Greatest JSON weapon is Serde with over 4.4K stars on github and a massive developer community. This is considered a core Rust library for every developer to learn in BRC's opinion

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically

Rust 버전 JsonPath 구현으로 Webassembly와 Javascript에서도 유사한 API 인터페이스를 제공 한다

JsonPath 구현으로 Webassembly와 Javascript에서도 유사한 API 인터페이스를 제공 한다

Rust 버전 JsonPath 구현으로 Webassembly와 Javascript에서도 유사한 API 인터페이스를 제공 한다

SIMD JSON for Rust  

Rust port of extremely fast serde compatibility

SIMD JSON for Rust  

JSON-E Rust data-struct paramter crate for lightweight embedded content with objects and much more

What makes JSON-e unique is that it extensive documentation and ease of use

JSON-E Rust data-struct paramter crate for lightweight embedded content with objects and much more
JSON

111

A Rust JSON5 serializer and deserializer which speaks Serde

Deserialize a JSON5 string with from_str

A Rust JSON5 serializer and deserializer which speaks Serde

Rust JSON Parser Benchmark

Download and Generate JSON Data

Rust JSON Parser Benchmark

Read JSON values quickly - Rust JSON Parser

AJSON get json value with specified path, such as project

Read JSON values quickly - Rust JSON Parser

Rust actix json request example

Send a json request to actix, and parse it

Rust actix json request example
JSON

140

json_typegen - Rust types from JSON samples

json_typegen is a collection of tools for generating types from

json_typegen - Rust types from JSON samples

Rust JSON parsing benchmarks

This project aims to provide benchmarks to show how various JSON-parsing libraries in the Rust programming language perform at various JSON-parsing tasks

Rust JSON parsing benchmarks

A tiny command line tool written in rust to print json data as a formatted...

A tiny command line tool written in rust to print json data as a formatted table

A tiny command line tool written in rust to print json data as a formatted...

A Rust implementation of the JSON-RPC 2

A Rust implementation of the serde) for JSON-RPC

A Rust implementation of the JSON-RPC 2
Facebook Instagram Twitter GitHub Dribbble
Privacy