-
-
Notifications
You must be signed in to change notification settings - Fork 15
feat(stackable-telemetry): Add RollingFileAppender #933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f26fb40
feat: add RollingFileAppender to stackable-telemetry
xeniape 0e16812
add changelog entry
xeniape be484ea
adjust docs
xeniape ca0ae20
adjust docs
xeniape 4d3ebd0
Merge branch 'main' into feat/add-rolling-file-appender
xeniape bfe89a7
error handling, simplify filelogsettings creation
xeniape 3676dc1
Merge branch 'main' into feat/add-rolling-file-appender
xeniape 6bb857b
Apply suggestions from code review
xeniape b927c87
Merge branch 'main' into feat/add-rolling-file-appender
xeniape b369fb2
fix use statement
xeniape b641f0f
Update crates/stackable-telemetry/src/tracing/mod.rs
xeniape 0e2226f
Merge branch 'main' into feat/add-rolling-file-appender
xeniape 6a32cdf
Merge branch 'main' into feat/add-rolling-file-appender
xeniape File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
crates/stackable-telemetry/src/tracing/settings/file_log.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
//! File Log Subscriber Settings. | ||
|
||
use std::{ops::Deref, path::PathBuf}; | ||
|
||
use super::{Build, Settings, SettingsBuilder}; | ||
|
||
/// Configure specific settings for the File Log subscriber. | ||
#[derive(Debug, Default, PartialEq)] | ||
pub struct FileLogSettings { | ||
/// Common subscriber settings that apply to the File Log Subscriber. | ||
pub common_settings: Settings, | ||
|
||
/// Path to directory for log files. | ||
pub file_log_dir: PathBuf, | ||
} | ||
|
||
impl Deref for FileLogSettings { | ||
type Target = Settings; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.common_settings | ||
} | ||
} | ||
|
||
/// This trait is only used for the typestate builder and cannot be implemented | ||
/// outside of this crate. | ||
/// | ||
/// The only reason it has pub visibility is because it needs to be at least as | ||
/// visible as the types that use it. | ||
#[doc(hidden)] | ||
pub trait BuilderState: private::Sealed {} | ||
|
||
/// This private module holds the [`Sealed`][1] trait that is used by the | ||
/// [`BuilderState`], so that it cannot be implemented outside of this crate. | ||
/// | ||
/// We impl Sealed for any types that will use the trait that we want to | ||
/// restrict impls on. In this case, the [`BuilderState`] trait. | ||
/// | ||
/// [1]: private::Sealed | ||
#[doc(hidden)] | ||
mod private { | ||
use super::*; | ||
|
||
pub trait Sealed {} | ||
|
||
impl Sealed for builder_state::PreLogDir {} | ||
impl Sealed for builder_state::Config {} | ||
} | ||
|
||
/// This module holds the possible states that the builder is in. | ||
/// | ||
/// Each state will implement [`BuilderState`] (with no methods), and the | ||
/// Builder struct ([`FileLogSettingsBuilder`][1]) itself will be implemented with | ||
/// each state as a generic parameter. | ||
/// This allows only the methods to be called when the builder is in the | ||
/// applicable state. | ||
/// | ||
/// [1]: super::FileLogSettingsBuilder | ||
#[doc(hidden)] | ||
pub mod builder_state { | ||
/// The initial state, before the log directory path is set. | ||
#[derive(Default)] | ||
pub struct PreLogDir; | ||
|
||
/// The state that allows you to configure the [`FileLogSettings`][1]. | ||
/// | ||
/// [1]: super::FileLogSettings | ||
#[derive(Default)] | ||
pub struct Config; | ||
} | ||
|
||
// Make the states usable | ||
#[doc(hidden)] | ||
impl BuilderState for builder_state::PreLogDir {} | ||
|
||
#[doc(hidden)] | ||
impl BuilderState for builder_state::Config {} | ||
|
||
/// For building [`FileLogSettings`]. | ||
/// | ||
/// <div class="warning"> | ||
/// Do not use directly, instead use the [`Settings::builder`] associated function. | ||
/// </div> | ||
pub struct FileLogSettingsBuilder<S: BuilderState> { | ||
pub(crate) common_settings: Settings, | ||
pub(crate) file_log_dir: Option<PathBuf>, | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Allow the generic to be used (needed for impls). | ||
_marker: std::marker::PhantomData<S>, | ||
} | ||
|
||
impl FileLogSettingsBuilder<builder_state::PreLogDir> { | ||
/// Set the directory for log files. | ||
/// | ||
/// A directory is required for using the File Log subscriber. | ||
pub fn with_file_log_dir(self, path: String) -> FileLogSettingsBuilder<builder_state::Config> { | ||
FileLogSettingsBuilder { | ||
common_settings: self.common_settings, | ||
file_log_dir: Some(PathBuf::from(path)), | ||
_marker: std::marker::PhantomData, | ||
} | ||
} | ||
Techassi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
impl FileLogSettingsBuilder<builder_state::Config> { | ||
/// Consumes self and returns a valid [`FileLogSettings`] instance. | ||
pub fn build(self) -> FileLogSettings { | ||
FileLogSettings { | ||
common_settings: self.common_settings, | ||
file_log_dir: self | ||
.file_log_dir | ||
.expect("file_log_dir must be configured at this point"), | ||
} | ||
} | ||
} | ||
|
||
/// This implementation is used to turn the common settings builder into the file log specific | ||
/// settings builder via the [`SettingsBuilder::file_log_settings_builder`] function. | ||
impl From<SettingsBuilder> for FileLogSettingsBuilder<builder_state::PreLogDir> { | ||
fn from(value: SettingsBuilder) -> Self { | ||
Self { | ||
common_settings: value.build(), | ||
file_log_dir: None, | ||
_marker: std::marker::PhantomData, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use tracing::level_filters::LevelFilter; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn builds_settings() { | ||
let expected = FileLogSettings { | ||
common_settings: Settings { | ||
environment_variable: "hello", | ||
default_level: LevelFilter::DEBUG, | ||
enabled: true, | ||
}, | ||
file_log_dir: PathBuf::from("/logs"), | ||
}; | ||
let result = Settings::builder() | ||
.with_environment_variable("hello") | ||
.with_default_level(LevelFilter::DEBUG) | ||
.enabled(true) | ||
.file_log_settings_builder() | ||
.with_file_log_dir(String::from("/logs")) | ||
.build(); | ||
|
||
assert_eq!(expected, result); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.