Skip to content
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

Question: Logging target #344

Closed
JRAndreassen opened this issue Aug 26, 2020 · 6 comments
Closed

Question: Logging target #344

JRAndreassen opened this issue Aug 26, 2020 · 6 comments

Comments

@JRAndreassen
Copy link

As a followup to "Damonizing and encrypting with Abscissa"..

Since I will be running as a daemon, I need to change some logging options:

  • redirect all logging activity to file
  • Change output formatting (Time format, add threads etc)
    Is there a way to do that? I've been digging but don't see a way short of modifying "application" and "trace/component"...

Thanks
JR

@PRTGC
Copy link

PRTGC commented Aug 28, 2020

@tarcieri :
I have a basic rolling file appender based on tracing-appender...
It is just an adaptation of your component...
It is not perfect, but it works...
There are a couple of PR's on "tracing-appender" that would be beneficial
Different Rorationss (Size & duration) and custom templates...
I would probably lobby for adding these parameters (log type, rotation)to the config

I can create a PR, but you may want to review before that...

//! Abscissa tracing file-appender component

// cargo.toml: tracing-appender = "*"

use std::path::PathBuf;
use tracing_log::LogTracer;
use tracing_subscriber::{
    layer::Layered,
    fmt::{ //Formatter, 
        Layer, 
        format::{
            DefaultFields,
            Format, Full,
            },
        time::SystemTime,
        },
    Registry,
    reload::Handle, EnvFilter, 
};
pub use tracing_appender::{
    rolling::{
        RollingFileAppender,
        Rotation,
     },
     non_blocking::{ NonBlocking, WorkerGuard},
    };

use super::config::Config;
use crate::{Component, FrameworkError, FrameworkErrorKind};

/// Abscissa component for initializing the `tracing` subsystem
#[derive(Component, Debug)]
#[component(core)]
pub struct TracingAppender {
//    filter_handle: Handle<EnvFilter, Formatter>,
    filter_handle: Handle<EnvFilter, Layered<Layer<Registry, DefaultFields, Format<Full, SystemTime>, NonBlocking>, Registry, Registry>>,
    worker_guard: WorkerGuard,
}
// Handle<EnvFilter, Layered<Layer<Registry, DefaultFields, Format<Full, SystemTime>, NonBlocking>, Registry, Registry>>
//layer::Layered<fmt_layer::Layer<Registry, N, E, W>, Registry>;

impl TracingAppender {
    /// Create a new [`Tracing`] component from the given [`Config`].
    pub fn new(config: Config, roll: Rotation, path: &PathBuf, file_name: &PathBuf) -> Result<Self, FrameworkError> {

        let file_appender = 
            RollingFileAppender::new(roll, path, file_name);
        let (non_blocking_appender, worker_guard) = 
            tracing_appender::non_blocking(file_appender);        
        // Configure log/tracing interoperability by setting a `LogTracer` as
        // the global logger for the log crate, which converts all log events
        // into tracing events.
        LogTracer::init().map_err(|e| FrameworkErrorKind::ComponentError.context(e))?;

        // Construct a tracing subscriber with the supplied filter and enable reloading.
        let builder = tracing_subscriber::fmt()
            .with_writer(non_blocking_appender)
            .with_env_filter(config.filter)
            .with_filter_reloading();
        let filter_handle = builder.reload_handle();
        let subscriber = builder.finish();

        // Now set it as the global tracing subscriber and save the handle.
        tracing::subscriber::set_global_default(subscriber)
            .map_err(|e| FrameworkErrorKind::ComponentError.context(e))?;

        info!("Initialized {:?} {:?}", path, file_name);

        Ok(Self { filter_handle, worker_guard })
    }

    /// Return the currently-active tracing filter.
    pub fn filter(&self) -> String {
        self.filter_handle
            .with_current(|filter| filter.to_string())
            .expect("the subscriber is not dropped before the component is")
    }

    /// Reload the currently-active filter with the supplied value.
    ///
    /// This can be used to provide a dynamic tracing filter endpoint.
    pub fn reload_filter(&mut self, filter: impl Into<EnvFilter>) {
        self.filter_handle
            .reload(filter)
            .expect("the subscriber is not dropped before the component is");
    }
}

@tony-iqlusion
Copy link
Member

I can create a PR...

I think it'd probably be better for you to maintain this as part of your own application or publish as a separate crate, unless there are framework changes strictly necessary to make it work. If there are, we can discuss what changes are necessary.

The "preferred" way to solve this issue with Abscissa is using existing system level init and logging infrastructure. In the case of systemd, that'd be systemd-journald+journald.

@JRAndreassen
Copy link
Author

@tony-iqlusion ,

Most of my clients are on a Windows platform so I have to use a local log.
And since it will be a Windows service, I have no console... So it has to go to disk,..

I tried to have implement it only in my crate, but
There was a couple of things that were "pub(crate)" that blocked it...
(I think it was: #[component(core)] , it's been a while...)

It is working though..

@tony-iqlusion
Copy link
Member

tony-iqlusion commented Oct 7, 2020

If there are specific changes to Abscissa you'd like for your logging needs, let us know.

@JRAndreassen
Copy link
Author

@tony-iqlusion 👍 Thanks for getting back to me...
It is working for now...
I'll figure out the specifics when I get my head above water...
and post. But, like I said, it was mostly around access to private's...

@tony-iqlusion
Copy link
Member

Closing this in favor of #402. I think there are updates needed in the logging subsystem, but I think #402 better captures a path forward on them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants