Skip to content

feat: logs filter - #95

Merged
vladopajic merged 20 commits into
masterfrom
disable-logs
Aug 1, 2026
Merged

feat: logs filter#95
vladopajic merged 20 commits into
masterfrom
disable-logs

Conversation

@vladopajic

@vladopajic vladopajic commented Jul 29, 2026

Copy link
Copy Markdown
Member
  • adds log filter
  • all tutorials disable filter to show clean output

closes: #79

@vladopajic
vladopajic marked this pull request as draft July 29, 2026 17:03

@gmelodie gmelodie left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

reviewing as i was almost done before it got converted back

Comment thread src/plugin.h Outdated
Comment thread src/plugin.cpp Outdated
fprintf(stderr, "Failed to disable libp2p logs: %s\n",
logRes.error.c_str());
return 1;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

If we get rid of the #ifdef..#endif stuff from above:

Suggested change
}
node.setLogLevel(LogLevel::None);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

hmm i don't see what #ifdef..#endif has to do with this code?

result from setLogLevel has to be checked because cbind returns result.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I guess the overall issue here is that set log level is creating ctx? Why is that. I would expect setLogLevel to never fail (i.e. do not create a context, require one and that's it).

This would keep us from doing

  if (!logRes.success) {
        fprintf(stderr, "Failed to disable libp2p logs: %s\n",
                logRes.error.c_str());
        return 1;
    }

every time

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

https://github.com/vacp2p/nim-libp2p/blob/a70c615c287a94e1134f45c05c18c338c7a3bc8f/cbind/libp2p.nim#L417

^ this is setLogLevel from cbind. that setLogLevel returns results, so this result has to be checked. this is what we are doing here when checking result.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

pr #2899 makes setLogLevel a static function. this change requires that call sites continue to check the result, despite avoiding the context.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

there is no validation when setting log lvl anymore, therefore no result (response).
validation is done on cbind side before libp2p node is created.
in logos module it will be impossible to set invalid value, as user will have to use LogLevel enum.

Comment thread flake.nix
inputs = {
logos-module-builder.url = "github:logos-co/logos-module-builder";
libp2p.url = "github:vacp2p/nim-libp2p/fix/cbind/nim-ffi-bump";
libp2p.url = "github:vacp2p/nim-libp2p/master";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

don't fix this to master, as changes can spill here unexpectedly

Suggested change
libp2p.url = "github:vacp2p/nim-libp2p/master";
libp2p.url = "github:vacp2p/nim-libp2p/fix/cbind/nim-ffi-bump";

or the commit that was merged in master

Suggested change
libp2p.url = "github:vacp2p/nim-libp2p/master";
libp2p.url = "github:vacp2p/nim-libp2p/a760255635529adbc2e8f198b8b53ba13396c4ec";

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

changes can't spill here unexpectedly as it is fixed to master, with rev a70c615c287a94e1134f45c05c18c338c7a3bc8f.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

see flake.lock file

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes! but when doing nix flake update to update some other dep this will also update nim-libp2p to latest master, which might not be what we want

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

why would it be okay for other deps and not for nim-libp2p dep?

@gmelodie gmelodie Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's say we need to update logos core, but nim-libp2p has some breaking changes that have not been ported to cbind yet.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

nim-libp2p has some breaking changes that have not been ported to cbind yet

could never happen. every push to nim-libp2p master has to pass CI. if nim-libp2p code brakes cbind CI that PR will not get through.

ether way, even when we assume that, that case can happen, those are exceptional cases. meaning that by default we want default behavior, which is nix flake update to updated everything. and exceptional cases are dealt with an exception. in this hypothetical case, one would still do nix flake update and manually rivert changes related to nim-libp2p.

@vladopajic
vladopajic marked this pull request as ready for review July 29, 2026 17:28
@vladopajic

Copy link
Copy Markdown
Member Author

hmmm i still see logs in tutorials... draft again

@vladopajic
vladopajic marked this pull request as draft July 29, 2026 17:46
Comment thread src/plugin.cpp Outdated
Comment on lines +217 to +223
if (!ctx) {
auto created = createContext();
if (!created.success) return created;
}
return callSync("Failed to set log level", [&](SyncPromise* p) {
return libp2p_ctx_set_log_level(ctx, static_cast<int64_t>(level),
&Libp2pModuleImpl::cbBool, p);

@richard-ramos richard-ramos Jul 29, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@gmelodie with nim-ffi, is it always required to have a context? I am wondering if we could just update nim-libp2p libp2pSetLogLevel function so it looks like:

proc libp2pSetLogLevel*(level: int): Future[Result[bool, string]] {.ffi.} =
  ## Changes the process-wide Chronicles runtime log level.
  if level < ord(low(chronicles.LogLevel)) or level > ord(high(chronicles.LogLevel)):
    return err("invalid log level: " & $level)

  when chronicles.runtimeFilteringEnabled:
    chronicles.setLogLevel(chronicles.LogLevel(level))
    ok(true)
  else:
    err("Chronicles runtime filtering is disabled")

and that way you avoid allocating memory for context and also breaking createNode.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes it should be possible to just do that

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yeah, this will make things better. i'll push this to nim-libp2p

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LogLevel is now passed via config, avoiding need for context.

Comment on lines +51 to +55
In your own application, `LogLevel::Error` or `LogLevel::Fatal` is often a
useful default. It keeps normal output quiet while still surfacing conditions
that may indicate libp2p is misbehaving or that your integration code needs an
adjustment. Some error logs describe remote-peer behavior, retries, or
recoverable internal state, so they may not require any action from your side.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

added this as a practical suggestion because users should not generally disable logs completely, especially while building or integrating an application.

still not fully sure this should be the final recommendation, because libp2p logs are not currently structured in a way that is consistently helpful to end users. there are cases where an error log may be written to stdout, but the condition is not necessarily actionable for the application. in some cases, the log level appears to be error mainly because the log statement is inside an except SomeError block.

second reason for adding this note is to raise the topic of improving (more accurately creating) a systematic approach to choosing log levels. runtime logging should be treated as a library feature with user-facing behavior. and now when we are not only a developers, but users as well, it may be easier for us to understand how to approach this.

this may also expand the scope of the commitment ift-ts:p2p:ift:2026q3-nimlibp2p-log-noise-reduction:logging-changes.

.... but it might be completely unnecessary in age of ai. but even with this assumption systematic approach would be useful to ai.

@vladopajic
vladopajic marked this pull request as ready for review July 31, 2026 10:15
@vladopajic

Copy link
Copy Markdown
Member Author
image

@vladopajic
vladopajic merged commit a31c40d into master Aug 1, 2026
10 checks passed
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

Successfully merging this pull request may close these issues.

disable libp2p logs in tutorials

3 participants