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

The default "source" module determined by Logger/LogHandler is usually wrong #145

Closed
gwynne opened this issue Jul 7, 2020 · 6 comments
Closed
Labels
kind/bug Feature doesn't work as expected. kind/enhancement Improvements to existing feature.

Comments

@gwynne
Copy link
Contributor

gwynne commented Jul 7, 2020

Expected behavior

Given this package layout:

.
|- Package.swift
 |- Sources
  |- ModuleA
   |- Subdir1
    |- Impl.swift
  |- ModuleB
   |- Subdir1
    |- Impl.swift

If I perform someLogger.info("hello") in ModuleA/Subdir1/Impl.swift and someLogger.info("world") in ModuleB/Subdir1/Impl.swift, I expect that the source parameters which reach the underlying LogHandler will be, respectively, ModuleA and ModuleB.

Actual behavior

In practice, the source parameter will be Subdir1 in both calls.

SwiftLog version/commit hash

SwiftLog 1.3.0

Swift & OS version (output of swift --version && uname -a)

Apple Swift version 5.3 (swiftlang-1200.0.16.9 clang-1200.0.22.5)
Target: x86_64-apple-darwin19.5.0
Darwin macbookpro.local 19.5.0 Darwin Kernel Version 19.5.0: Tue May 26 20:41:44 PDT 2020; root:xnu-6153.121.2~2/RELEASE_X86_64 x86_64

Further information

This is the natural result of using what is essentially basename(dirname(#file)) as the default for source in Logger.currentModule(filePath:) (

internal static func currentModule(filePath: String = #file) -> String {
) and Logger.log(level:_:metadata:source:file:function:line:) (
source: source() ?? Logger.currentModule(filePath: (file)),
). It assumes that all or most packages will use a flat structure for their source files beneath the "per-module" level. This is not the case for the vast majority of packages in, e.g., Vapor (see any of several repositories listed under https://github.com/vapor/ ), and in other common usage in large projects. Because this default is declared on Logger's log() method, it is difficult to override it via, for example, a project's choice of LogHandler, with the result that this new source parameter (otherwise a very useful input) must be ignored in favor of continuing to use such problematic solutions as parsing #file or #filePath (the irony that this behavior is also the cause of this very issue is not lost on me!).

@ktoso ktoso added kind/bug Feature doesn't work as expected. kind/enhancement Improvements to existing feature. labels Jul 7, 2020
@ktoso
Copy link
Member

ktoso commented Jul 7, 2020

Damn, feeling silly this slipped through when we were talking about source for weeks 😥 And in my projects I have tons of sub-directories like that as well.

Will ponder some more soon and try to see if a "real" solution is anywhere near in sight...

@ktoso ktoso added this to the 1.3.1 milestone Jul 8, 2020
@ktoso
Copy link
Member

ktoso commented Jul 8, 2020

Marked as 1.3.1 but I don't think we'll actually solve it there, just a reminder for myself to poke around some more.

@ktoso ktoso removed this from the 1.3.1 milestone Jul 15, 2020
@porglezomp
Copy link

Is this easy to fix now that we have #fileID which should always start with Module/?

@ktoso
Copy link
Member

ktoso commented Mar 9, 2022

Do you want to send in a PR @porglezomp ? That'd help a lot :-)

@porglezomp
Copy link

It looks like this is actually already fixed in Swift 5.3+, with PR #187.
The behavior is tested in LoggingTest.testLogsEmittedFromSubdirectoryGetCorrectModuleInNewerSwifts, so I think it's safe to close this issue?

@ktoso
Copy link
Member

ktoso commented Jan 18, 2023

Excellent, thanks for the info and sorry I missed the reply here way back then :)

@ktoso ktoso closed this as completed Jan 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Feature doesn't work as expected. kind/enhancement Improvements to existing feature.
Projects
None yet
Development

No branches or pull requests

3 participants