-
Notifications
You must be signed in to change notification settings - Fork 869
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
consider System.Logger
in related hints/code-generation
#8240
Comments
parts of this might be already customizable by templates. But I agree there could be likely out-of-the-box improvements made. |
If this is touched, other logging frameworks should also be considered. SLF4J/Log4J2/Logback/commons-logging all can be valid selections for logging. It would be good if this would be per project. |
In addition to stuff mentioned in the OP, there is also stuff in In particular, These could be copied to But I notice that none of these templates have/use Also, there's a hint
is converted to
Could add an additional, probably better, fix:
|
Before An anecdote along these lines: There a library project I occasionally work on which used |
SLF4J existed for how long? Sorry, but I see https://xkcd.com/927/ here. Just as the module system of java faces an uphill battle, the same is true for logging systems. I have projects where the framework mandates logback, a CMS that mandates SLF4J and has legacy use of log4j, local projects that use JUL. Only the latter could be changed, all others have to integrate with their corresponding ecosystem. |
yes, although I don't know how many are visible through that window. Things like #3458 etc are usually all solved with templates.
yep, updating this hint would be a good approach (e.g making it more configurable), hint settings can also optionally be per-project -> this solves multiple issues at once. The hint can also not simply look at the classpath and try to "do the right thing", since a typical dusty enterprise project has one logging facade but logging bridges to 5+ logging impls - every second lib can use a different impl. So everything might be in the classpath. It likely has to be a user choice set somewhere. |
My limited experience, with anything other than JUL, is with a library that was developed as part of much larger application. The app used log4j2 and it was used by the library. We can probably all agree that a library should use a facade and not a specific logger. Another selling point of using System.Logger is reducing the library's depency on 3rd party artifacts. I understand that projects have requirements for style/libraries/logging, but I don't understand:
After changing the library (in the example) to System.Logger, and adding the log4j2 bridge to the main app, it integrated fine into the existing ecosystem without any changes to the log4j2 configuration files and produced identical log output. But that's all beside the point. A facility to add/register arbitrary 3rd party logging frameworks/facades and their code generation templates/actions and a UI to specify which one to use when NB generates logging code doesn't sound like a bad idea. The most popular logging methods would be provided as reference implementations. It may be that extending existing capability with JDK's System.Logger is not worth it because of the possibility of re-architecting how NB generates logging. |
sure but in practice it doesn't really matter. For every logging lib a bridge lib exists by now. Logging is a getter with something what holds the log level fields. This can be (luckily) bridged to any other logging impl without having to convince lib authors to use dedicated facades. The project I am looking at right now uses: jcl-over-slf4j, log4-to-slf4j, jul-to-slf4j and and eclipse logger bridge which maps to slf4j. The "impl" is just yet another bridge, pretending to be a slf4j impl which logs directly to JFR.
I wouldn't make this too extensible. Luckily the java community decided at some point to stop developing more logging libs. "Generate -> logger" could offer a list. The hint would also need a few more options. |
IIUC, If NB want to support code generation for 3rd party logging, IMHO anything less than an extensible infrastructure misses the mark. Different projects probably have different standards. I wouldn't be surprised if different projects that use the same backend logger have different standards for how the logging is used. I wonder how many projects are around that still use custom logging implementations. In NB, there is not only code generation, but additional hints that look for standards/style violations (I've added a list from NB of things I'm aware to the end of the OP). Each logger, or logger style, that is added should specify not only code generation, but also standard checks and rewrites for use with existing hints. Additional and/or custom logger hints should also be considered that are useful for enterprise projects. Might want to register a set of hints that convert from one logger framework to another. It may be that per project specification is too coarse grained (thinking about some of the previous comments in this thread); and so a set of roots should be specified for a given logger/style. It took me a little while but I finally remember that NetBeans has |
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>2.0.16</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.24.3</version>
</dependency>
</dependencies>
then it can offer more options for the code gen actions - sure. |
Whoa, wow, yeah, OK. That works in many situations where a "sole JUL handler" is OK. Thanks.
Making 3rd party logging a "first class" option in NB needs more that just code gen. |
I think |
This default is also used by `Generate > Logger...`. Addresses some issues in raised in apache#8240. New feature `Use existing declared Logger`.
Forgot to mention that the PR is fully functional. (at least that's the idea). It's DRAFT while looking for answers to make sure corner cases are correctly handled. |
This default is also used by `Generate > Logger...`. Addresses some issues in raised in apache#8240. New feature `Use existing declared Logger`. Fix "Logging" hints to handle System.Logger.
This default is also used by `Generate > Logger...`. Addresses some issues in raised in apache#8240. New feature `Use existing declared Logger`. Fix "Logging" hints to handle System.Logger.
Ready for review... |
This default is also used by `Generate > Logger...`. Addresses some issues in raised in apache#8240. New feature `Use existing declared Logger`. Fix "Logging" hints to handle System.Logger.
This default is also used by `Generate > Logger...`. Addresses some issues in raised in apache#8240. New feature `Use existing declared Logger`. Fix "Logging" hints to handle System.Logger.
This default is also used by `Generate > Logger...`. Addresses some issues in raised in apache#8240. New feature `Use existing declared Logger`. Fix "Logging" hints to handle System.Logger.
This default is also used by `Generate > Logger...`. Addresses some issues in raised in apache#8240. New feature `Use existing declared Logger`. Fix "Logging" hints to handle System.Logger.
Description
This affects (the hints are under
Options > Editor > Hints > Java
)Insert Code
action/dialog, akaGenerate > Logger...
Surround with try-catch
hint, underError Fixes
hintsLogging
hintsThere might be other items that should be added to the above list.
A key question is how to specify which Logger, between
System
andjava.util
, to use. CouldTools > Options
, examplesTools > Options > Java > Java Logging
Tools > Options > Java > Java
for future lookingTools > Options > Editor > Hints > Java > Surround with try-catch
with
Use java.lang.System.Logger
Doing (2) above is the simplest. I'd be inclined to put it above
java.util
and have it check-marked by default. Consider that in most instances at run-time, there is little practical difference from the current behavior. These options could be used byGenerate > Logger...
, and in other cases as needed, to select which of the two loggers to use to initialize the logger. If this is acceptable, I volunteer.A few additional things that could be done
A "use existing logger" option for
Surround with try-catch
.For reference, here the current and proposed code.
Current
Generate>Logger...
private static final Logger LOG = Logger.getLogger(Mavenproject5.class.getName());
Surround with try-catch
Logger.getLogger(Mavenproject5.class.getName()).log(Level.SEVERE, null, ex);
Proposed (when option is for System.Logger)
Generate>Logger...
private static final Logger LOG = System.getLogger(Mavenproject5.class.getName());
Surround with try-catch
System.getLogger(Mavenproject5.class.getName()).log(Level.ERROR, null, ex);
LOG.log(Level.ERROR, null, ex);
// If "use existing logger" implementedAdditional
Generate>Logger...
options for considerationWith these options, a generated logger would look like
Additional features
Added 2025-02-16
JUL
toSystem.Logger
. Use withRefactor > Inspect and Transform...
.Other existing NetBeans logger stuff
Added 2025-02-16
If there's missing things, LMK and I'll include them here
Logging
hints categoryCode Templates
Use case/motivation
Provide access to newer, as of Java-9, capability
Related issues
No response
Are you willing to submit a pull request?
Yes
The text was updated successfully, but these errors were encountered: