From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <g.goller@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id F2B699A472
 for <pbs-devel@lists.proxmox.com>; Fri, 13 Oct 2023 14:36:54 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id D26DF1E312
 for <pbs-devel@lists.proxmox.com>; Fri, 13 Oct 2023 14:36:24 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pbs-devel@lists.proxmox.com>; Fri, 13 Oct 2023 14:36:23 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 7FBDA48F12
 for <pbs-devel@lists.proxmox.com>; Fri, 13 Oct 2023 14:36:23 +0200 (CEST)
Content-Type: multipart/alternative;
 boundary="------------yahbtRR0kTr310uez2wQ2cq5"
Message-ID: <aab6ffa6-4ac4-45d2-a533-66109ebf73ec@proxmox.com>
Date: Fri, 13 Oct 2023 14:36:22 +0200
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
From: Gabriel Goller <g.goller@proxmox.com>
To: pbs-devel@lists.proxmox.com
Reply-To: Proxmox Backup Server development discussion
 <pbs-devel@lists.proxmox.com>
References: <20231011140102.273423-1-g.goller@proxmox.com>
 <20231011140102.273423-3-g.goller@proxmox.com>
Content-Language: en-US
In-Reply-To: <20231011140102.273423-3-g.goller@proxmox.com>
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.337 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 HTML_MESSAGE            0.001 HTML included in message
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [lib.rs]
Subject: Re: [pbs-devel] [RFC proxmox 2/2] proxmox-log: added tracing infra
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 13 Oct 2023 12:36:55 -0000

This is a multi-part message in MIME format.
--------------yahbtRR0kTr310uez2wQ2cq5
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Some things that are gonna get fixed with v2:

On 10/11/23 16:01, Gabriel Goller wrote:
> [..]
> +
> +tokio::task_local! {
> +    pub static LOGGER: RefCell<Option<FileLogger>>;
> +    pub static WARN_COUNTER: RefCell<Option<u64>>;
> +}
Move the `task_local!` stuff to the lib.rs file, that is
probably cleaner...
> [..]
> +
> +pub fn init_logger(
> +    env_var_name: &str,
> +    default_log_level: LevelFilter,
> +    application_name: &str,
> +) -> Result<(), anyhow::Error> {
> +    let mut log_level = default_log_level;
> +    if let Ok(v) = env::var(env_var_name) {
> +        if let Ok(l) = v.parse::<LevelFilter>() {
> +            log_level = l;
> +        }
> +    }
> +    let registry = tracing_subscriber::registry()
> +        .with(FilelogLayer::new().with_filter(filter_fn(|metadata| {
> +            metadata.fields().field("tasklog").is_some()
> +        })))
> +        .with(SyslogLayer::new(application_name.to_string()).with_filter(log_level));
> +
We also need the `.with_filter(log_level)` on the `FilelogLayer`, so
that the PBS_LOG variable also sets the log level on the task logs.
> +    tracing::subscriber::set_global_default(registry)?;
> +
> +    LogTracer::init()?;

Use `init_with_filter` here, so that we also set the loglevel on the 
(underlying) `log`
implementation. Like that a `log::info` call for example doesn't even
forward the message to `tracing`.

> +    Ok(())
> +}
> [..]
--------------yahbtRR0kTr310uez2wQ2cq5
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Some things that are gonna get fixed with v2:<br>
    </p>
    <div class="moz-cite-prefix">On 10/11/23 16:01, Gabriel Goller
      wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:20231011140102.273423-3-g.goller@proxmox.com">
      <pre class="moz-quote-pre" wrap="">[..]
+
+tokio::task_local! {
+    pub static LOGGER: RefCell&lt;Option&lt;FileLogger&gt;&gt;;
+    pub static WARN_COUNTER: RefCell&lt;Option&lt;u64&gt;&gt;;
+}</pre>
    </blockquote>
    Move the `task_local!` stuff to the lib.rs file, that is<br>
    probably cleaner...<span style="white-space: pre-wrap">
</span>
    <blockquote type="cite"
      cite="mid:20231011140102.273423-3-g.goller@proxmox.com">
      <pre class="moz-quote-pre" wrap="">[..]
+
+pub fn init_logger(
+    env_var_name: &amp;str,
+    default_log_level: LevelFilter,
+    application_name: &amp;str,
+) -&gt; Result&lt;(), anyhow::Error&gt; {
+    let mut log_level = default_log_level;
+    if let Ok(v) = env::var(env_var_name) {
+        if let Ok(l) = v.parse::&lt;LevelFilter&gt;() {
+            log_level = l;
+        }
+    }
+    let registry = tracing_subscriber::registry()
+        .with(FilelogLayer::new().with_filter(filter_fn(|metadata| {
+            metadata.fields().field("tasklog").is_some()
+        })))
+        .with(SyslogLayer::new(application_name.to_string()).with_filter(log_level));
+</pre>
    </blockquote>
    We also need the `.with_filter(log_level)` on the `FilelogLayer`, so<br>
    that the PBS_LOG variable also sets the log level on the task logs.<br>
    <blockquote type="cite"
      cite="mid:20231011140102.273423-3-g.goller@proxmox.com">
      <pre class="moz-quote-pre" wrap="">
+    tracing::subscriber::set_global_default(registry)?;
+
+    LogTracer::init()?;</pre>
    </blockquote>
    <p>Use `init_with_filter` here, so that we also set the loglevel on
      the (underlying) `log`<br>
      implementation. Like that a `log::info` call for example doesn't
      even<br>
      forward the message to `tracing`.<br>
    </p>
    <blockquote type="cite"
      cite="mid:20231011140102.273423-3-g.goller@proxmox.com">
      <pre class="moz-quote-pre" wrap="">
+    Ok(())
+}
[..]
</pre>
    </blockquote>
  </body>
</html>

--------------yahbtRR0kTr310uez2wQ2cq5--