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

JUL logger configuration may be lost before usage due to GC #17217

Closed
acmi opened this issue Jun 17, 2019 · 2 comments
Closed

JUL logger configuration may be lost before usage due to GC #17217

acmi opened this issue Jun 17, 2019 · 2 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@acmi
Copy link

acmi commented Jun 17, 2019

// java version "12.0.1" 2019-04-16
// Java(TM) SE Runtime Environment (build 12.0.1+12)
// Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)
// 
// org.springframework.boot:spring-boot:2.1.5.RELEASE


import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.java.JavaLoggingSystem;

import java.util.logging.Level;
import java.util.logging.Logger;

var logger = "some.logger";

var loggingSystem = new JavaLoggingSystem(getClass().getClassLoader());
loggingSystem.setLogLevel(logger, LogLevel.TRACE);

// Just configured logger works as expected.
assert Logger.getLogger(logger).isLoggable(Level.FINEST);

// But configuration and usage are usually separated in time,
// GC may happen between `LoggingApplicationListener.initializeFinalLoggingLevels` 
// and obtaining logger (e.g. static log field initialization).
// JUL LogManager uses weak references to store loggers.
// Spring JavaLoggingSystem doesn't hold strong references to configured loggers.
System.gc();

// So configuration may be lost :(
assert Logger.getLogger(logger).isLoggable(Level.FINEST);

Maybe JavaLoggingSystem should keep strong references to configured loggers?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 17, 2019
@philwebb philwebb added the for: team-attention An issue we'd like other members of the team to review label Jun 17, 2019
@wilkinsona
Copy link
Member

wilkinsona commented Jun 17, 2019

From the javadoc of Logger.getLogger(String):

Note: The LogManager may only retain a weak reference to the newly created Logger. It is important to understand that a previously created Logger with the given name may be garbage collected at any time if there is no strong reference to the Logger. In particular, this means that two back-to-back calls like getLogger("MyLogger").log(...) may use different Logger objects named "MyLogger" if there is no strong reference to the Logger named "MyLogger" elsewhere in the program.

The use of weak references was introduced in a bug fix in Java 7 and back-ported to Java 6. See JDK-6274920 for details.

@acmi
Copy link
Author

acmi commented Jun 17, 2019

Yep, and it leads to this problem.
please check example: https://github.com/acmi/jul_test

@philwebb philwebb added type: bug A general bug and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Jun 19, 2019
@philwebb philwebb added this to the 2.1.x milestone Jun 19, 2019
@wilkinsona wilkinsona self-assigned this Jul 9, 2019
@wilkinsona wilkinsona modified the milestones: 2.1.x, 2.1.7 Jul 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants