Skip to content

Conversation

@hyperfinitism
Copy link
Owner

@hyperfinitism hyperfinitism commented Aug 28, 2025

Summaries

This PR overhauls the logging functionality, enabling flexible logging and log file output.

What's changed

Component Change Highlights
crates/orcid-fetcher-logger New flexible logging + optional log file output
crates/orcid-works-cli Changed replace tracing with orcid-fetcher-logger
crates/orcid-works-model Unchanged

How to try it

# display log level: Trace
orcid-works-cli -i "0000-0002-1825-0097" -o output.json -vv

# display log level: Debug
orcid-works-cli -i "0000-0002-1825-0097" -o output.json -v

# display log level: Info
orcid-works-cli -i "0000-0002-1825-0097" -o output.json

# display log level: Warn
orcid-works-cli -i "0000-0002-1825-0097" -o output.json -q

# display log level: Error
orcid-works-cli -i "0000-0002-1825-0097" -o output.json -qq

# display log level: None
orcid-works-cli -i "0000-0002-1825-0097" -o output.json -qqq

# display log level: Info
# logfile log level: Trace
orcid-works-cli -i "0000-0002-1825-0097" -o output.json -l trace.log

Summary by Sourcery

Integrate a new flexible logging crate and revamp CLI logging by replacing tracing with log macros, exposing verbosity and log file options, and enhancing error reporting with unique IDs and contextual messages

New Features:

  • Introduce orcid-fetcher-logger crate providing configurable console verbosity and optional trace log file output
  • Add CLI flags for adjusting console log level (-v/-q) and specifying a log file path (-l)

Enhancements:

  • Replace tracing subscriber instrumentation in orcid-works-cli with orcid-fetcher-logger and standard log macros
  • Enhance error handling by generating unique error identifiers and adding contextual debug logs in IO and API modules
  • Improve JSON read/write routines with contextual error messages and atomic file operations

Documentation:

  • Update README with descriptions of verbose, quiet, and log file flags

* creates the `orcid-fetcher-logger` crate based on `flexi_logger`
* replace `tracing` with `orcid-fetcher-logger`
* enhance logging functionality: flexible logging + logfile output
@hyperfinitism hyperfinitism self-assigned this Aug 28, 2025
@hyperfinitism hyperfinitism added the enhancement New feature or request label Aug 28, 2025
@sourcery-ai
Copy link

sourcery-ai bot commented Aug 28, 2025

Reviewer's Guide

This PR introduces a new orcid-fetcher-logger crate providing console-level configuration and optional file output via flexi_logger, and refactors the orcid-works-cli to replace tracing with standard log macros, add verbosity and log-file flags, and enhance error reporting with UUID-tagged logs.

Sequence diagram for logger initialization in orcid-works-cli

sequenceDiagram
    participant main
    participant Cli
    participant orcid_fetcher_logger
    main->>Cli: parse CLI arguments
    main->>orcid_fetcher_logger: console_level(verbose, quiet)
    main->>orcid_fetcher_logger: init_logger(console_level, log)
    orcid_fetcher_logger-->>main: logger initialized or error
Loading

Class diagram for updated Cli struct in orcid-works-cli

classDiagram
    class Cli {
        +id: String
        +out: PathBuf
        +concurrency: u32
        +rate_limit: u32
        +user_agent_note: Option<String>
        +force_fetch: bool
        +verbose: u8
        +quiet: u8
        +log: Option<PathBuf>
    }
Loading

File-Level Changes

Change Details Files
Add orcid-fetcher-logger crate for flexible logging and optional log file output
  • Implemented console_level and init_logger functions using flexi_logger
  • Defined crate Cargo.toml and added to workspace members
  • Enabled duplication to stderr at configured console level
crates/orcid-fetcher-logger/src/lib.rs
crates/orcid-fetcher-logger/Cargo.toml
Cargo.toml
Refactor orcid-works-cli to use log-based logging, add verbosity flags and UUID-enhanced error traces
  • Replaced tracing_subscriber and tracing macros with init_logger and log::{info,debug,warn,error}
  • Added -v/-vv and -q/-qq verbosity flags and --log file option to CLI
  • Enhanced JSON I/O and HTTP API error handlers with UUID-tagged logs and debug chains
  • Updated dependencies: removed tracing, added orcid-fetcher-logger, flexi_logger, log, uuid
crates/orcid-works-cli/src/main.rs
crates/orcid-works-cli/src/api.rs
crates/orcid-works-cli/src/io.rs
crates/orcid-works-cli/Cargo.toml
README.md

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `crates/orcid-works-cli/src/main.rs:131` </location>
<code_context>
 #[tokio::main]
 async fn main() -> Result<()> {
-    tracing_subscriber::fmt().init();
+    let cli = Cli::parse();
+
+    // initialise logger
</code_context>

<issue_to_address>
Cli is parsed twice, which may lead to inconsistent state or wasted computation.

Pass the parsed CLI arguments from `main` to `run` to avoid duplicate parsing and ensure consistency.
</issue_to_address>

### Comment 2
<location> `crates/orcid-works-cli/src/io.rs:105` </location>
<code_context>
-        return Err(e);
+    if let Err(e) = temp.persist(path).map_err(|e| e.error) {
+        let eid = Uuid::new_v4();
+        debug!("E[{eid}] error: e");
+        error!("E[{eid}] atomic rename failure path={}", path.display());
+        return Err(e).with_context(|| {
</code_context>

<issue_to_address>
The debug log prints a literal 'e' instead of the error value.

Change the log statement to use `{e}` so the actual error is printed.
</issue_to_address>

### Comment 3
<location> `crates/orcid-works-cli/src/api.rs:48` </location>
<code_context>
+        }
+    };

     if res.error_for_status_ref().is_err() {
         let status = res.status();
         let body = res.text().await.unwrap_or_default();
</code_context>

<issue_to_address>
The error body is always read, even for large responses, which may impact performance.

Limit the size of the response body read for error reporting, or truncate logged output to reduce memory usage.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
        let status = res.status();
        let body = res.text().await.unwrap_or_default();

        let eid = Uuid::new_v4();
        error!("E[{eid}] HTTP {status}");
        debug!("E[{eid}] url={url}");
        debug!("E[{eid}] response body={body}");
        bail!("E[{eid}] HTTP {status}");
=======
        use tokio::io::AsyncReadExt;
        use std::cmp;

        let status = res.status();
        // Limit error body to 4096 bytes
        let mut body = String::new();
        let mut stream = res.bytes_stream();
        let mut total_bytes = 0;
        const MAX_ERROR_BODY: usize = 4096;
        while let Some(chunk) = stream.next().await {
            match chunk {
                Ok(bytes) => {
                    let take = cmp::min(MAX_ERROR_BODY - total_bytes, bytes.len());
                    body.push_str(&String::from_utf8_lossy(&bytes[..take]));
                    total_bytes += take;
                    if total_bytes >= MAX_ERROR_BODY {
                        break;
                    }
                }
                Err(_) => break,
            }
        }
        let truncated = total_bytes >= MAX_ERROR_BODY;

        let eid = Uuid::new_v4();
        error!("E[{eid}] HTTP {status}");
        debug!("E[{eid}] url={url}");
        if truncated {
            debug!("E[{eid}] response body (truncated to {MAX_ERROR_BODY} bytes)={body}");
        } else {
            debug!("E[{eid}] response body={body}");
        }
        bail!("E[{eid}] HTTP {status}");
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@hyperfinitism hyperfinitism merged commit 66aae5a into main Oct 3, 2025
8 checks passed
@hyperfinitism hyperfinitism deleted the feat/flexible-logging branch October 3, 2025 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants