Skip to content

Commit

Permalink
1.0 Release (#154)
Browse files Browse the repository at this point in the history
* Expose errors as failure::Error (#138)

* Issue #129: Drop XML Config support (#137)

* Rename file feature (#147)

* Rename to config_parsing

* Add workflows for devel

* Update changelog

* Add an init function which takes a RawConfig (#150)

* Clippy

* Changelog, cleanup

* json macro

* qualify

* typo

* Remove unused dir from .gitignore

* Init raw on all platforms

* Remove log4rs::FormatError::XmlFeatureFlagRequired (#156)

* Expand env vars in the path for File and RollingFile appenders (#155)

* Reorganize config (#157)

* Reorganize config

* allow missing docs so tests run

* lint abatement

* some renames

* Use anyhow/thiserror (#159)

* Use anyhow/thiserror

* Change addtivity to additive to match the actual code (#163)

* Errors 1.0 (#160)

Co-authored-by: shmapdy <richard.meester@gmail.com>

* clippy

* Standard derives rebase (#175)

* Bump serde-value

* Alpha 1 version

* Update highlight colors to be the same as env_logger (#167)

* Custom err handler (#183)

* WIP custom err handling

* Compiling

* Add an  method to the public api

* re-enable Debug

* bump ver

* fix: init_raw_config forcing max_log_level to Info (#200)

Co-authored-by: braindead <braindeaded@protonmail.com>

* pattern encoder: Set trace to default color, reset formatting after (#186)

* pattern encoder: Set trace to default color, reset formatting after

* Fix formatting of last commit

* lol apparently rustfmt doesn't like commas? idk if this will work

* Set trace color to cyan

Co-authored-by: Richard M <gadunga@users.noreply.github.com>
Co-authored-by: shmapdy <richard.meester@gmail.com>
Co-authored-by: Charles Giguere <IceSentry@users.noreply.github.com>
Co-authored-by: 1c7718e7 <shirokovMartin@gmail.com>
Co-authored-by: braindead <braindeaded@protonmail.com>
Co-authored-by: Julia DeMille <8127111+judemille@users.noreply.github.com>
  • Loading branch information
7 people committed Dec 16, 2020
1 parent 656c9fb commit d637eea
Show file tree
Hide file tree
Showing 28 changed files with 912 additions and 833 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ on:
push:
branches:
- master
- devel
pull_request:
branches:
- master
- devel

jobs:
lint:
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
target/
Cargo.lock
!codegen/Cargo.lock
.idea/
*.iml
.vscode/
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,20 @@

## [Unreleased]


### New

* Custom error handling
* Allow parsing of config from string
* Expand env vars in file path of file and RollingFile appenders PR#155

### Changed

* Colors changed to match `env_logger`
* Drop XML config support
* Rename feature `file` to `config_parsing`
* Use `thiserror`/`anyhow` for errors

### Fixed

## [0.13.0]
Expand Down
27 changes: 14 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "log4rs"
version = "0.13.0"
version = "1.0.0-alpha-2"
authors = ["Steven Fackler <sfackler@gmail.com>", "Evan Simmons <esims89@gmail.com>"]
description = "A highly configurable multi-output logging implementation for the `log` facade"
license = "MIT/Apache-2.0"
Expand All @@ -10,22 +10,21 @@ keywords = ["log", "logger", "logging", "log4"]
edition = '2018'

[features]
default = ["all_components", "file", "yaml_format", "gzip"]
default = ["all_components", "config_parsing", "yaml_format"]

file = ["humantime", "serde", "serde_derive", "serde-value", "typemap", "log/serde"]
config_parsing = ["humantime", "serde", "serde-value", "typemap", "log/serde"]
yaml_format = ["serde_yaml"]
json_format = ["serde_json"]
toml_format = ["toml"]
xml_format = ["serde-xml-rs"]

console_appender = ["console_writer", "simple_writer", "pattern_encoder"]
file_appender = ["parking_lot", "simple_writer", "pattern_encoder"]
rolling_file_appender = ["parking_lot", "simple_writer", "pattern_encoder"]
file_appender = ["parking_lot", "simple_writer", "pattern_encoder", "regex"]
rolling_file_appender = ["parking_lot", "simple_writer", "pattern_encoder", "regex"]
compound_policy = []
delete_roller = []
fixed_window_roller = []
size_trigger = []
json_encoder = ["serde", "serde_json", "chrono", "log-mdc", "serde_derive", "log/serde", "thread-id"]
json_encoder = ["serde", "serde_json", "chrono", "log-mdc", "log/serde", "thread-id"]
pattern_encoder = ["chrono", "log-mdc", "thread-id"]
ansi_writer = []
console_writer = ["ansi_writer", "libc", "winapi"]
Expand Down Expand Up @@ -57,19 +56,21 @@ arc-swap = "0.4"
chrono = { version = "0.4", optional = true }
flate2 = { version = "1.0", optional = true }
fnv = "1.0"
humantime = { version = "1.0", optional = true }
humantime = { version = "2.0", optional = true }
log = { version = "0.4.0", features = ["std"] }
log-mdc = { version = "0.1", optional = true }
serde = { version = "1.0", optional = true }
serde_derive = { version = "1.0", optional = true }
serde-value = { version = "0.6", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }
serde-value = { version = "0.7", optional = true }
thread-id = { version = "3.3", optional = true }
typemap = { version = "0.3", optional = true }
serde_json = { version = "1.0", optional = true }
serde_yaml = { version = "0.8.4", optional = true }
toml = { version = "0.5", optional = true }
serde-xml-rs = { version = "0.4", optional = true }
parking_lot = { version = "0.11.0", optional = true }
regex = { version = "1", optional = true }
thiserror = "1.0.15"
anyhow = "1.0.28"
derivative = "2.1.1"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", optional = true, features = ["handleapi", "minwindef", "processenv", "winbase", "wincon"] }
Expand All @@ -80,7 +81,7 @@ libc = { version = "0.2", optional = true }
[dev-dependencies]
lazy_static = "1.4"
streaming-stats = "0.2.3"
humantime = "1.0.0"
humantime = "2.0"
tempfile = "3.1.0"

[[example]]
Expand Down
39 changes: 17 additions & 22 deletions src/append/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
//!
//! Requires the `console_appender` feature.

use derivative::Derivative;
use log::Record;
#[cfg(feature = "file")]
use serde_derive::Deserialize;
use std::{
error::Error,
fmt,
io::{self, Write},
};

#[cfg(feature = "file")]
#[cfg(feature = "config_parsing")]
use crate::config::{Deserialize, Deserializers};
#[cfg(feature = "config_parsing")]
use crate::encode::EncoderConfig;
#[cfg(feature = "file")]
use crate::file::{Deserialize, Deserializers};
use crate::{
append::Append,
encode::{
Expand All @@ -30,16 +28,16 @@ use crate::{
};

/// The console appender's configuration.
#[cfg(feature = "file")]
#[derive(Deserialize)]
#[cfg(feature = "config_parsing")]
#[serde(deny_unknown_fields)]
#[derive(Debug, serde::Deserialize)]
pub struct ConsoleAppenderConfig {
target: Option<ConfigTarget>,
encoder: Option<EncoderConfig>,
}

#[cfg(feature = "file")]
#[derive(Deserialize)]
#[cfg(feature = "config_parsing")]
#[derive(Debug, serde::Deserialize)]
enum ConfigTarget {
#[serde(rename = "stdout")]
Stdout,
Expand Down Expand Up @@ -109,21 +107,16 @@ impl<'a> encode::Write for WriterLock<'a> {
///
/// It supports output styling if standard out is a console buffer on Windows
/// or is a TTY on Unix.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct ConsoleAppender {
#[derivative(Debug = "ignore")]
writer: Writer,
encoder: Box<dyn Encode>,
}

impl fmt::Debug for ConsoleAppender {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("ConsoleAppender")
.field("encoder", &self.encoder)
.finish()
}
}

impl Append for ConsoleAppender {
fn append(&self, record: &Record) -> Result<(), Box<dyn Error + Sync + Send>> {
fn append(&self, record: &Record) -> anyhow::Result<()> {
let mut writer = self.writer.lock();
self.encoder.encode(&mut writer, record)?;
writer.flush()?;
Expand Down Expand Up @@ -187,6 +180,7 @@ impl ConsoleAppenderBuilder {
}

/// The stream to log to.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Target {
/// Standard output.
Stdout,
Expand All @@ -208,10 +202,11 @@ pub enum Target {
/// encoder:
/// kind: pattern
/// ```
#[cfg(feature = "file")]
#[cfg(feature = "config_parsing")]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
pub struct ConsoleAppenderDeserializer;

#[cfg(feature = "file")]
#[cfg(feature = "config_parsing")]
impl Deserialize for ConsoleAppenderDeserializer {
type Trait = dyn Append;

Expand All @@ -221,7 +216,7 @@ impl Deserialize for ConsoleAppenderDeserializer {
&self,
config: ConsoleAppenderConfig,
deserializers: &Deserializers,
) -> Result<Box<dyn Append>, Box<dyn Error + Sync + Send>> {
) -> anyhow::Result<Box<dyn Append>> {
let mut appender = ConsoleAppender::builder();
if let Some(target) = config.target {
let target = match target {
Expand Down
47 changes: 24 additions & 23 deletions src/append/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,47 @@
//!
//! Requires the `file_appender` feature.

use derivative::Derivative;
use log::Record;
use parking_lot::Mutex;
#[cfg(feature = "file")]
use serde_derive::Deserialize;
use std::{
error::Error,
fmt,
fs::{self, File, OpenOptions},
io::{self, BufWriter, Write},
path::{Path, PathBuf},
};

#[cfg(feature = "file")]
#[cfg(feature = "config_parsing")]
use crate::config::{Deserialize, Deserializers};
#[cfg(feature = "config_parsing")]
use crate::encode::EncoderConfig;
#[cfg(feature = "file")]
use crate::file::{Deserialize, Deserializers};

use crate::{
append::Append,
encode::{pattern::PatternEncoder, writer::simple::SimpleWriter, Encode},
};

/// The file appender's configuration.
#[cfg(feature = "file")]
#[derive(Deserialize)]
#[cfg(feature = "config_parsing")]
#[serde(deny_unknown_fields)]
#[derive(Clone, Eq, PartialEq, Hash, Debug, Default, serde::Deserialize)]
pub struct FileAppenderConfig {
path: String,
encoder: Option<EncoderConfig>,
append: Option<bool>,
}

/// An appender which logs to a file.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct FileAppender {
path: PathBuf,
#[derivative(Debug = "ignore")]
file: Mutex<SimpleWriter<BufWriter<File>>>,
encoder: Box<dyn Encode>,
}

impl fmt::Debug for FileAppender {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.debug_struct("FileAppender")
.field("file", &self.path)
.field("encoder", &self.encoder)
.finish()
}
}

impl Append for FileAppender {
fn append(&self, record: &Record) -> Result<(), Box<dyn Error + Sync + Send>> {
fn append(&self, record: &Record) -> anyhow::Result<()> {
let mut file = self.file.lock();
self.encoder.encode(&mut *file, record)?;
file.flush()?;
Expand Down Expand Up @@ -92,8 +84,12 @@ impl FileAppenderBuilder {
}

/// Consumes the `FileAppenderBuilder`, producing a `FileAppender`.
/// The path argument can contain environment variables of the form $ENV{name_here},
/// where 'name_here' will be the name of the environment variable that
/// will be resolved. Note that if the variable fails to resolve,
/// $ENV{name_here} will NOT be replaced in the path.
pub fn build<P: AsRef<Path>>(self, path: P) -> io::Result<FileAppender> {
let path = path.as_ref().to_owned();
let path = super::env_util::expand_env_vars(path.as_ref().to_path_buf());
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
Expand Down Expand Up @@ -122,6 +118,10 @@ impl FileAppenderBuilder {
/// kind: file
///
/// # The path of the log file. Required.
/// # The path can contain environment variables of the form $ENV{name_here},
/// # where 'name_here' will be the name of the environment variable that
/// # will be resolved. Note that if the variable fails to resolve,
/// # $ENV{name_here} will NOT be replaced in the path.
/// path: log/foo.log
///
/// # Specifies if the appender should append to or truncate the log file if it
Expand All @@ -132,10 +132,11 @@ impl FileAppenderBuilder {
/// encoder:
/// kind: pattern
/// ```
#[cfg(feature = "file")]
#[cfg(feature = "config_parsing")]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Default)]
pub struct FileAppenderDeserializer;

#[cfg(feature = "file")]
#[cfg(feature = "config_parsing")]
impl Deserialize for FileAppenderDeserializer {
type Trait = dyn Append;

Expand All @@ -145,7 +146,7 @@ impl Deserialize for FileAppenderDeserializer {
&self,
config: FileAppenderConfig,
deserializers: &Deserializers,
) -> Result<Box<Self::Trait>, Box<dyn Error + Sync + Send>> {
) -> anyhow::Result<Box<Self::Trait>> {
let mut appender = FileAppender::builder();
if let Some(append) = config.append {
appender = appender.append(append);
Expand Down
Loading

0 comments on commit d637eea

Please sign in to comment.