Why in System.Logger is log(DEBUG, msg) used instead of debug(msg)?
I am reading System.Logger API that was introduced in Java 9. And I can't understand why they developed so strange
API:
System.Logger {
public default void log(Level level, String msg){...}
}
I call it strange
because all popular logging frameworks (I know) don't put level as argument, but name the calling method by level name. For example:
//Log4j
logger.error("This is error : " + parameter);
//SLF4J
logger.debug("Printing variable value: {}", variable);
//apache.commons.logging
log.debug(Object message);
//and even sun.util.logging.PlatformLogger
logger.warning(String msg)
How to explain it?
java logging java-9
|
show 2 more comments
I am reading System.Logger API that was introduced in Java 9. And I can't understand why they developed so strange
API:
System.Logger {
public default void log(Level level, String msg){...}
}
I call it strange
because all popular logging frameworks (I know) don't put level as argument, but name the calling method by level name. For example:
//Log4j
logger.error("This is error : " + parameter);
//SLF4J
logger.debug("Printing variable value: {}", variable);
//apache.commons.logging
log.debug(Object message);
//and even sun.util.logging.PlatformLogger
logger.warning(String msg)
How to explain it?
java logging java-9
Maybe because#log
method has many overloads (consuming Object, String, String + Objects, String + Throwable, Supplier), so making all combinations would be cumbersome?
– Alex Salauyou
Nov 14 '18 at 13:21
@AlexSalauyou See slf4j logger - slf4j.org/api/org/slf4j/Logger.html. For example debug method is overloaded 10 times.
– Pavel_K
Nov 14 '18 at 13:23
4
It's hard to answer questions about developers intentions unless you are the developer. Note that this is not intended to be a logging platform that you use, but rather an interface that allows you to connect the system logging to whatever logging platform you use. So ease of implementation is important.
– RealSkeptic
Nov 14 '18 at 13:35
2
As someone who runs Fortify scans on code, my guess would be it's more of a security perspective. When you add .debug() calls in your code, you are adding debugging logic that goes to production. While they are intrinsically the same, a parameter value can be changed during run-time, so passing a Level parameter may not be considered or flagged as being debug logic in your code base.
– ChrisThompson
Nov 14 '18 at 14:32
1
Opinion based: I would second the thought that the current syntax seems more flexible in moving logs from one level to another based on some configuration which in current logging frameworks you can only set the Level to be logged and what would be the Level of the code you select. You do not have the flexibility to saylog.debug("This shall be logged as INFO sometime tomorrow");
<~ just for an example.
– nullpointer
Nov 14 '18 at 18:47
|
show 2 more comments
I am reading System.Logger API that was introduced in Java 9. And I can't understand why they developed so strange
API:
System.Logger {
public default void log(Level level, String msg){...}
}
I call it strange
because all popular logging frameworks (I know) don't put level as argument, but name the calling method by level name. For example:
//Log4j
logger.error("This is error : " + parameter);
//SLF4J
logger.debug("Printing variable value: {}", variable);
//apache.commons.logging
log.debug(Object message);
//and even sun.util.logging.PlatformLogger
logger.warning(String msg)
How to explain it?
java logging java-9
I am reading System.Logger API that was introduced in Java 9. And I can't understand why they developed so strange
API:
System.Logger {
public default void log(Level level, String msg){...}
}
I call it strange
because all popular logging frameworks (I know) don't put level as argument, but name the calling method by level name. For example:
//Log4j
logger.error("This is error : " + parameter);
//SLF4J
logger.debug("Printing variable value: {}", variable);
//apache.commons.logging
log.debug(Object message);
//and even sun.util.logging.PlatformLogger
logger.warning(String msg)
How to explain it?
java logging java-9
java logging java-9
edited Nov 14 '18 at 18:47
nullpointer
48.5k11101195
48.5k11101195
asked Nov 14 '18 at 13:04
Pavel_KPavel_K
2,69742070
2,69742070
Maybe because#log
method has many overloads (consuming Object, String, String + Objects, String + Throwable, Supplier), so making all combinations would be cumbersome?
– Alex Salauyou
Nov 14 '18 at 13:21
@AlexSalauyou See slf4j logger - slf4j.org/api/org/slf4j/Logger.html. For example debug method is overloaded 10 times.
– Pavel_K
Nov 14 '18 at 13:23
4
It's hard to answer questions about developers intentions unless you are the developer. Note that this is not intended to be a logging platform that you use, but rather an interface that allows you to connect the system logging to whatever logging platform you use. So ease of implementation is important.
– RealSkeptic
Nov 14 '18 at 13:35
2
As someone who runs Fortify scans on code, my guess would be it's more of a security perspective. When you add .debug() calls in your code, you are adding debugging logic that goes to production. While they are intrinsically the same, a parameter value can be changed during run-time, so passing a Level parameter may not be considered or flagged as being debug logic in your code base.
– ChrisThompson
Nov 14 '18 at 14:32
1
Opinion based: I would second the thought that the current syntax seems more flexible in moving logs from one level to another based on some configuration which in current logging frameworks you can only set the Level to be logged and what would be the Level of the code you select. You do not have the flexibility to saylog.debug("This shall be logged as INFO sometime tomorrow");
<~ just for an example.
– nullpointer
Nov 14 '18 at 18:47
|
show 2 more comments
Maybe because#log
method has many overloads (consuming Object, String, String + Objects, String + Throwable, Supplier), so making all combinations would be cumbersome?
– Alex Salauyou
Nov 14 '18 at 13:21
@AlexSalauyou See slf4j logger - slf4j.org/api/org/slf4j/Logger.html. For example debug method is overloaded 10 times.
– Pavel_K
Nov 14 '18 at 13:23
4
It's hard to answer questions about developers intentions unless you are the developer. Note that this is not intended to be a logging platform that you use, but rather an interface that allows you to connect the system logging to whatever logging platform you use. So ease of implementation is important.
– RealSkeptic
Nov 14 '18 at 13:35
2
As someone who runs Fortify scans on code, my guess would be it's more of a security perspective. When you add .debug() calls in your code, you are adding debugging logic that goes to production. While they are intrinsically the same, a parameter value can be changed during run-time, so passing a Level parameter may not be considered or flagged as being debug logic in your code base.
– ChrisThompson
Nov 14 '18 at 14:32
1
Opinion based: I would second the thought that the current syntax seems more flexible in moving logs from one level to another based on some configuration which in current logging frameworks you can only set the Level to be logged and what would be the Level of the code you select. You do not have the flexibility to saylog.debug("This shall be logged as INFO sometime tomorrow");
<~ just for an example.
– nullpointer
Nov 14 '18 at 18:47
Maybe because
#log
method has many overloads (consuming Object, String, String + Objects, String + Throwable, Supplier), so making all combinations would be cumbersome?– Alex Salauyou
Nov 14 '18 at 13:21
Maybe because
#log
method has many overloads (consuming Object, String, String + Objects, String + Throwable, Supplier), so making all combinations would be cumbersome?– Alex Salauyou
Nov 14 '18 at 13:21
@AlexSalauyou See slf4j logger - slf4j.org/api/org/slf4j/Logger.html. For example debug method is overloaded 10 times.
– Pavel_K
Nov 14 '18 at 13:23
@AlexSalauyou See slf4j logger - slf4j.org/api/org/slf4j/Logger.html. For example debug method is overloaded 10 times.
– Pavel_K
Nov 14 '18 at 13:23
4
4
It's hard to answer questions about developers intentions unless you are the developer. Note that this is not intended to be a logging platform that you use, but rather an interface that allows you to connect the system logging to whatever logging platform you use. So ease of implementation is important.
– RealSkeptic
Nov 14 '18 at 13:35
It's hard to answer questions about developers intentions unless you are the developer. Note that this is not intended to be a logging platform that you use, but rather an interface that allows you to connect the system logging to whatever logging platform you use. So ease of implementation is important.
– RealSkeptic
Nov 14 '18 at 13:35
2
2
As someone who runs Fortify scans on code, my guess would be it's more of a security perspective. When you add .debug() calls in your code, you are adding debugging logic that goes to production. While they are intrinsically the same, a parameter value can be changed during run-time, so passing a Level parameter may not be considered or flagged as being debug logic in your code base.
– ChrisThompson
Nov 14 '18 at 14:32
As someone who runs Fortify scans on code, my guess would be it's more of a security perspective. When you add .debug() calls in your code, you are adding debugging logic that goes to production. While they are intrinsically the same, a parameter value can be changed during run-time, so passing a Level parameter may not be considered or flagged as being debug logic in your code base.
– ChrisThompson
Nov 14 '18 at 14:32
1
1
Opinion based: I would second the thought that the current syntax seems more flexible in moving logs from one level to another based on some configuration which in current logging frameworks you can only set the Level to be logged and what would be the Level of the code you select. You do not have the flexibility to say
log.debug("This shall be logged as INFO sometime tomorrow");
<~ just for an example.– nullpointer
Nov 14 '18 at 18:47
Opinion based: I would second the thought that the current syntax seems more flexible in moving logs from one level to another based on some configuration which in current logging frameworks you can only set the Level to be logged and what would be the Level of the code you select. You do not have the flexibility to say
log.debug("This shall be logged as INFO sometime tomorrow");
<~ just for an example.– nullpointer
Nov 14 '18 at 18:47
|
show 2 more comments
1 Answer
1
active
oldest
votes
Questions about the intentions of developers are inherently difficult to answer, unless you are the developer.
That being said, we do have access to the original proposal for this feature - JEP 264.
The summary:
Define a minimal logging API which platform classes can use to log messages, together with a service interface for consumers of those messages. A library or application can provide an implementation of this service in order to route platform log messages to the logging framework of its choice. If no implementation is provided then a default implementation based upon the java.util.logging API is used.
From the goals:
Be easily adoptable by applications which use external logging framework, such as SLF4J or Log4J.
From the non-goals:
It is not a goal to define a general-purpose interface for logging. The service interface contains only the minimal set of methods that the JDK needs for its own usage.
So what we have here is not "Yet another logging framework" like SLF4J, Log4J, etc. What we have is an interface that allows you to tell the JVM to use the same logging tool that you are using for the classes in your application, for logging its own stuff.
The typical usage scenario would be an application which has a complex setup in, say, SLF4J, logging to console, files, databases, or sending text to phones. You want the JVM classes to use the same system. So you write an adapter - a class that implements the System.Logger
interface, using your SLF4J setup.
It's not that you can't use the current system logger for logging - you can - but that's not what it was created for. It was created for you to implement and set the system logger so that it calls your logging framework of choice.
In its current form, when you implement, you only need to implement four methods:
getName()
isLoggable(System.Logger.Level)
log(System.Logger.Level, ResourceBundle, String, Object...)
log(System.Logger.Level, ResourceBundle, String, Throwable)
Now, System.Logger.Level
has seven levels. Imagine if instead of having to implement two logging methods, you had to implement 14 logging methods? And more often than not, those implementation would look exactly the same, with one little name change. This is not wise.
As it stands, almost every existing logging framework has a log(level,...)
method, and then the implementation of System.Logger
's log(...)
can usually be done simply by mapping from the System.Logger.Level
to your framework's definition of Level.
And if you want to log a message?
Well, if you are using a complex logging platform, you log your message there directly, you don't need to go through the system logger. If you insist on using it - you'll need to use the levels as arguments or write your own wrappers. This is simply not the use case the developers had in mind.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300918%2fwhy-in-system-logger-is-logdebug-msg-used-instead-of-debugmsg%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Questions about the intentions of developers are inherently difficult to answer, unless you are the developer.
That being said, we do have access to the original proposal for this feature - JEP 264.
The summary:
Define a minimal logging API which platform classes can use to log messages, together with a service interface for consumers of those messages. A library or application can provide an implementation of this service in order to route platform log messages to the logging framework of its choice. If no implementation is provided then a default implementation based upon the java.util.logging API is used.
From the goals:
Be easily adoptable by applications which use external logging framework, such as SLF4J or Log4J.
From the non-goals:
It is not a goal to define a general-purpose interface for logging. The service interface contains only the minimal set of methods that the JDK needs for its own usage.
So what we have here is not "Yet another logging framework" like SLF4J, Log4J, etc. What we have is an interface that allows you to tell the JVM to use the same logging tool that you are using for the classes in your application, for logging its own stuff.
The typical usage scenario would be an application which has a complex setup in, say, SLF4J, logging to console, files, databases, or sending text to phones. You want the JVM classes to use the same system. So you write an adapter - a class that implements the System.Logger
interface, using your SLF4J setup.
It's not that you can't use the current system logger for logging - you can - but that's not what it was created for. It was created for you to implement and set the system logger so that it calls your logging framework of choice.
In its current form, when you implement, you only need to implement four methods:
getName()
isLoggable(System.Logger.Level)
log(System.Logger.Level, ResourceBundle, String, Object...)
log(System.Logger.Level, ResourceBundle, String, Throwable)
Now, System.Logger.Level
has seven levels. Imagine if instead of having to implement two logging methods, you had to implement 14 logging methods? And more often than not, those implementation would look exactly the same, with one little name change. This is not wise.
As it stands, almost every existing logging framework has a log(level,...)
method, and then the implementation of System.Logger
's log(...)
can usually be done simply by mapping from the System.Logger.Level
to your framework's definition of Level.
And if you want to log a message?
Well, if you are using a complex logging platform, you log your message there directly, you don't need to go through the system logger. If you insist on using it - you'll need to use the levels as arguments or write your own wrappers. This is simply not the use case the developers had in mind.
add a comment |
Questions about the intentions of developers are inherently difficult to answer, unless you are the developer.
That being said, we do have access to the original proposal for this feature - JEP 264.
The summary:
Define a minimal logging API which platform classes can use to log messages, together with a service interface for consumers of those messages. A library or application can provide an implementation of this service in order to route platform log messages to the logging framework of its choice. If no implementation is provided then a default implementation based upon the java.util.logging API is used.
From the goals:
Be easily adoptable by applications which use external logging framework, such as SLF4J or Log4J.
From the non-goals:
It is not a goal to define a general-purpose interface for logging. The service interface contains only the minimal set of methods that the JDK needs for its own usage.
So what we have here is not "Yet another logging framework" like SLF4J, Log4J, etc. What we have is an interface that allows you to tell the JVM to use the same logging tool that you are using for the classes in your application, for logging its own stuff.
The typical usage scenario would be an application which has a complex setup in, say, SLF4J, logging to console, files, databases, or sending text to phones. You want the JVM classes to use the same system. So you write an adapter - a class that implements the System.Logger
interface, using your SLF4J setup.
It's not that you can't use the current system logger for logging - you can - but that's not what it was created for. It was created for you to implement and set the system logger so that it calls your logging framework of choice.
In its current form, when you implement, you only need to implement four methods:
getName()
isLoggable(System.Logger.Level)
log(System.Logger.Level, ResourceBundle, String, Object...)
log(System.Logger.Level, ResourceBundle, String, Throwable)
Now, System.Logger.Level
has seven levels. Imagine if instead of having to implement two logging methods, you had to implement 14 logging methods? And more often than not, those implementation would look exactly the same, with one little name change. This is not wise.
As it stands, almost every existing logging framework has a log(level,...)
method, and then the implementation of System.Logger
's log(...)
can usually be done simply by mapping from the System.Logger.Level
to your framework's definition of Level.
And if you want to log a message?
Well, if you are using a complex logging platform, you log your message there directly, you don't need to go through the system logger. If you insist on using it - you'll need to use the levels as arguments or write your own wrappers. This is simply not the use case the developers had in mind.
add a comment |
Questions about the intentions of developers are inherently difficult to answer, unless you are the developer.
That being said, we do have access to the original proposal for this feature - JEP 264.
The summary:
Define a minimal logging API which platform classes can use to log messages, together with a service interface for consumers of those messages. A library or application can provide an implementation of this service in order to route platform log messages to the logging framework of its choice. If no implementation is provided then a default implementation based upon the java.util.logging API is used.
From the goals:
Be easily adoptable by applications which use external logging framework, such as SLF4J or Log4J.
From the non-goals:
It is not a goal to define a general-purpose interface for logging. The service interface contains only the minimal set of methods that the JDK needs for its own usage.
So what we have here is not "Yet another logging framework" like SLF4J, Log4J, etc. What we have is an interface that allows you to tell the JVM to use the same logging tool that you are using for the classes in your application, for logging its own stuff.
The typical usage scenario would be an application which has a complex setup in, say, SLF4J, logging to console, files, databases, or sending text to phones. You want the JVM classes to use the same system. So you write an adapter - a class that implements the System.Logger
interface, using your SLF4J setup.
It's not that you can't use the current system logger for logging - you can - but that's not what it was created for. It was created for you to implement and set the system logger so that it calls your logging framework of choice.
In its current form, when you implement, you only need to implement four methods:
getName()
isLoggable(System.Logger.Level)
log(System.Logger.Level, ResourceBundle, String, Object...)
log(System.Logger.Level, ResourceBundle, String, Throwable)
Now, System.Logger.Level
has seven levels. Imagine if instead of having to implement two logging methods, you had to implement 14 logging methods? And more often than not, those implementation would look exactly the same, with one little name change. This is not wise.
As it stands, almost every existing logging framework has a log(level,...)
method, and then the implementation of System.Logger
's log(...)
can usually be done simply by mapping from the System.Logger.Level
to your framework's definition of Level.
And if you want to log a message?
Well, if you are using a complex logging platform, you log your message there directly, you don't need to go through the system logger. If you insist on using it - you'll need to use the levels as arguments or write your own wrappers. This is simply not the use case the developers had in mind.
Questions about the intentions of developers are inherently difficult to answer, unless you are the developer.
That being said, we do have access to the original proposal for this feature - JEP 264.
The summary:
Define a minimal logging API which platform classes can use to log messages, together with a service interface for consumers of those messages. A library or application can provide an implementation of this service in order to route platform log messages to the logging framework of its choice. If no implementation is provided then a default implementation based upon the java.util.logging API is used.
From the goals:
Be easily adoptable by applications which use external logging framework, such as SLF4J or Log4J.
From the non-goals:
It is not a goal to define a general-purpose interface for logging. The service interface contains only the minimal set of methods that the JDK needs for its own usage.
So what we have here is not "Yet another logging framework" like SLF4J, Log4J, etc. What we have is an interface that allows you to tell the JVM to use the same logging tool that you are using for the classes in your application, for logging its own stuff.
The typical usage scenario would be an application which has a complex setup in, say, SLF4J, logging to console, files, databases, or sending text to phones. You want the JVM classes to use the same system. So you write an adapter - a class that implements the System.Logger
interface, using your SLF4J setup.
It's not that you can't use the current system logger for logging - you can - but that's not what it was created for. It was created for you to implement and set the system logger so that it calls your logging framework of choice.
In its current form, when you implement, you only need to implement four methods:
getName()
isLoggable(System.Logger.Level)
log(System.Logger.Level, ResourceBundle, String, Object...)
log(System.Logger.Level, ResourceBundle, String, Throwable)
Now, System.Logger.Level
has seven levels. Imagine if instead of having to implement two logging methods, you had to implement 14 logging methods? And more often than not, those implementation would look exactly the same, with one little name change. This is not wise.
As it stands, almost every existing logging framework has a log(level,...)
method, and then the implementation of System.Logger
's log(...)
can usually be done simply by mapping from the System.Logger.Level
to your framework's definition of Level.
And if you want to log a message?
Well, if you are using a complex logging platform, you log your message there directly, you don't need to go through the system logger. If you insist on using it - you'll need to use the levels as arguments or write your own wrappers. This is simply not the use case the developers had in mind.
answered Nov 14 '18 at 15:16
RealSkepticRealSkeptic
28.1k63262
28.1k63262
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300918%2fwhy-in-system-logger-is-logdebug-msg-used-instead-of-debugmsg%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Maybe because
#log
method has many overloads (consuming Object, String, String + Objects, String + Throwable, Supplier), so making all combinations would be cumbersome?– Alex Salauyou
Nov 14 '18 at 13:21
@AlexSalauyou See slf4j logger - slf4j.org/api/org/slf4j/Logger.html. For example debug method is overloaded 10 times.
– Pavel_K
Nov 14 '18 at 13:23
4
It's hard to answer questions about developers intentions unless you are the developer. Note that this is not intended to be a logging platform that you use, but rather an interface that allows you to connect the system logging to whatever logging platform you use. So ease of implementation is important.
– RealSkeptic
Nov 14 '18 at 13:35
2
As someone who runs Fortify scans on code, my guess would be it's more of a security perspective. When you add .debug() calls in your code, you are adding debugging logic that goes to production. While they are intrinsically the same, a parameter value can be changed during run-time, so passing a Level parameter may not be considered or flagged as being debug logic in your code base.
– ChrisThompson
Nov 14 '18 at 14:32
1
Opinion based: I would second the thought that the current syntax seems more flexible in moving logs from one level to another based on some configuration which in current logging frameworks you can only set the Level to be logged and what would be the Level of the code you select. You do not have the flexibility to say
log.debug("This shall be logged as INFO sometime tomorrow");
<~ just for an example.– nullpointer
Nov 14 '18 at 18:47