LOGGER::info is not consumed as s -> LOGGER.info(s)
I have defined a logger instance as follows:
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
I have an array of strings that I want to log, so I used the following:
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(LOGGER::info);
However, nothing is printed into the log. But, if I change the reference method to an equivalent lambda form, it prints into the log:
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(s -> LOGGER.info(s));
What am I missing here?
I tried the following approaches as well and they all behave the same:
Stream.of("ABC", "DEF", "XYZ").forEach(LOGGER::info);
List.of("ABC", "DEF", "XYZ").forEach(LOGGER::info);
Even Intellij IDE highlights s -> LOGGER.info(s) with yellow and suggests changing it to LOGGER::info.
I tried to define a custom class:
static class Consumer
{
void consume(String s)
{
LOGGER.info(s);
}
}
and use it in place of LOGGER::info:
Consumer consumer = new Consumer();
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(consumer::consume);
It prints into the log!
The Logger class has two overloaded methods:
void info(String msg)
and
void info(Supplier<String> msgSupplier)
but only the first is assignable to void forEach(Consumer<? super T> action), I believe.
java logging lambda java-stream method-reference
add a comment |
I have defined a logger instance as follows:
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
I have an array of strings that I want to log, so I used the following:
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(LOGGER::info);
However, nothing is printed into the log. But, if I change the reference method to an equivalent lambda form, it prints into the log:
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(s -> LOGGER.info(s));
What am I missing here?
I tried the following approaches as well and they all behave the same:
Stream.of("ABC", "DEF", "XYZ").forEach(LOGGER::info);
List.of("ABC", "DEF", "XYZ").forEach(LOGGER::info);
Even Intellij IDE highlights s -> LOGGER.info(s) with yellow and suggests changing it to LOGGER::info.
I tried to define a custom class:
static class Consumer
{
void consume(String s)
{
LOGGER.info(s);
}
}
and use it in place of LOGGER::info:
Consumer consumer = new Consumer();
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(consumer::consume);
It prints into the log!
The Logger class has two overloaded methods:
void info(String msg)
and
void info(Supplier<String> msgSupplier)
but only the first is assignable to void forEach(Consumer<? super T> action), I believe.
java logging lambda java-stream method-reference
Interesting... it's working for me well. What version of java did you use?
– Aleksandr Semyannikov
Nov 14 '18 at 13:51
I tried it on both Java 8 and Java 11. Currently, I am using Oracle JDK 11.0.1.
– Eng.Fouad
Nov 14 '18 at 13:52
Works for me on java 8u191
– Julio Daniel Reyes
Nov 14 '18 at 13:54
Arrays.stream(new String { "ABC", "DEF", "XYZ" }).forEach(LOGGER::info);works fine for me (Java 8), LOGGER being ajava.util.logging.Logger.
– Alex Shesterov
Nov 14 '18 at 13:54
add a comment |
I have defined a logger instance as follows:
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
I have an array of strings that I want to log, so I used the following:
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(LOGGER::info);
However, nothing is printed into the log. But, if I change the reference method to an equivalent lambda form, it prints into the log:
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(s -> LOGGER.info(s));
What am I missing here?
I tried the following approaches as well and they all behave the same:
Stream.of("ABC", "DEF", "XYZ").forEach(LOGGER::info);
List.of("ABC", "DEF", "XYZ").forEach(LOGGER::info);
Even Intellij IDE highlights s -> LOGGER.info(s) with yellow and suggests changing it to LOGGER::info.
I tried to define a custom class:
static class Consumer
{
void consume(String s)
{
LOGGER.info(s);
}
}
and use it in place of LOGGER::info:
Consumer consumer = new Consumer();
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(consumer::consume);
It prints into the log!
The Logger class has two overloaded methods:
void info(String msg)
and
void info(Supplier<String> msgSupplier)
but only the first is assignable to void forEach(Consumer<? super T> action), I believe.
java logging lambda java-stream method-reference
I have defined a logger instance as follows:
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
I have an array of strings that I want to log, so I used the following:
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(LOGGER::info);
However, nothing is printed into the log. But, if I change the reference method to an equivalent lambda form, it prints into the log:
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(s -> LOGGER.info(s));
What am I missing here?
I tried the following approaches as well and they all behave the same:
Stream.of("ABC", "DEF", "XYZ").forEach(LOGGER::info);
List.of("ABC", "DEF", "XYZ").forEach(LOGGER::info);
Even Intellij IDE highlights s -> LOGGER.info(s) with yellow and suggests changing it to LOGGER::info.
I tried to define a custom class:
static class Consumer
{
void consume(String s)
{
LOGGER.info(s);
}
}
and use it in place of LOGGER::info:
Consumer consumer = new Consumer();
Arrays.stream(new String{"ABC", "DEF", "XYZ"}).forEach(consumer::consume);
It prints into the log!
The Logger class has two overloaded methods:
void info(String msg)
and
void info(Supplier<String> msgSupplier)
but only the first is assignable to void forEach(Consumer<? super T> action), I believe.
java logging lambda java-stream method-reference
java logging lambda java-stream method-reference
edited Nov 14 '18 at 13:50
Eng.Fouad
asked Nov 14 '18 at 13:39
Eng.FouadEng.Fouad
84.9k53248362
84.9k53248362
Interesting... it's working for me well. What version of java did you use?
– Aleksandr Semyannikov
Nov 14 '18 at 13:51
I tried it on both Java 8 and Java 11. Currently, I am using Oracle JDK 11.0.1.
– Eng.Fouad
Nov 14 '18 at 13:52
Works for me on java 8u191
– Julio Daniel Reyes
Nov 14 '18 at 13:54
Arrays.stream(new String { "ABC", "DEF", "XYZ" }).forEach(LOGGER::info);works fine for me (Java 8), LOGGER being ajava.util.logging.Logger.
– Alex Shesterov
Nov 14 '18 at 13:54
add a comment |
Interesting... it's working for me well. What version of java did you use?
– Aleksandr Semyannikov
Nov 14 '18 at 13:51
I tried it on both Java 8 and Java 11. Currently, I am using Oracle JDK 11.0.1.
– Eng.Fouad
Nov 14 '18 at 13:52
Works for me on java 8u191
– Julio Daniel Reyes
Nov 14 '18 at 13:54
Arrays.stream(new String { "ABC", "DEF", "XYZ" }).forEach(LOGGER::info);works fine for me (Java 8), LOGGER being ajava.util.logging.Logger.
– Alex Shesterov
Nov 14 '18 at 13:54
Interesting... it's working for me well. What version of java did you use?
– Aleksandr Semyannikov
Nov 14 '18 at 13:51
Interesting... it's working for me well. What version of java did you use?
– Aleksandr Semyannikov
Nov 14 '18 at 13:51
I tried it on both Java 8 and Java 11. Currently, I am using Oracle JDK 11.0.1.
– Eng.Fouad
Nov 14 '18 at 13:52
I tried it on both Java 8 and Java 11. Currently, I am using Oracle JDK 11.0.1.
– Eng.Fouad
Nov 14 '18 at 13:52
Works for me on java 8u191
– Julio Daniel Reyes
Nov 14 '18 at 13:54
Works for me on java 8u191
– Julio Daniel Reyes
Nov 14 '18 at 13:54
Arrays.stream(new String { "ABC", "DEF", "XYZ" }).forEach(LOGGER::info); works fine for me (Java 8), LOGGER being a java.util.logging.Logger.– Alex Shesterov
Nov 14 '18 at 13:54
Arrays.stream(new String { "ABC", "DEF", "XYZ" }).forEach(LOGGER::info); works fine for me (Java 8), LOGGER being a java.util.logging.Logger.– Alex Shesterov
Nov 14 '18 at 13:54
add a comment |
1 Answer
1
active
oldest
votes
Consider that code:
Arrays.stream(new String{"ABC"}).forEach(LOGGER::info);
Arrays.stream(new String{"ABC"}).forEach(s -> LOGGER.info(s));
The difference is in the first case source of message is:
java.util.Spliterators$ArraySpliterator
in the second:
Main
Probably your logger has some filter that does not let logging events from java.util.Spliterators$ArraySpliterator class.
Try to check result of LOGGER.getFilter()
You got it right. I have a filter that prevents any logging other than from my own packages. Thanks.
– Eng.Fouad
Nov 14 '18 at 14:13
@Eng.Fouad glad to help!
– Aleksandr Semyannikov
Nov 14 '18 at 14:14
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%2f53301594%2floggerinfo-is-not-consumed-as-s-logger-infos%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
Consider that code:
Arrays.stream(new String{"ABC"}).forEach(LOGGER::info);
Arrays.stream(new String{"ABC"}).forEach(s -> LOGGER.info(s));
The difference is in the first case source of message is:
java.util.Spliterators$ArraySpliterator
in the second:
Main
Probably your logger has some filter that does not let logging events from java.util.Spliterators$ArraySpliterator class.
Try to check result of LOGGER.getFilter()
You got it right. I have a filter that prevents any logging other than from my own packages. Thanks.
– Eng.Fouad
Nov 14 '18 at 14:13
@Eng.Fouad glad to help!
– Aleksandr Semyannikov
Nov 14 '18 at 14:14
add a comment |
Consider that code:
Arrays.stream(new String{"ABC"}).forEach(LOGGER::info);
Arrays.stream(new String{"ABC"}).forEach(s -> LOGGER.info(s));
The difference is in the first case source of message is:
java.util.Spliterators$ArraySpliterator
in the second:
Main
Probably your logger has some filter that does not let logging events from java.util.Spliterators$ArraySpliterator class.
Try to check result of LOGGER.getFilter()
You got it right. I have a filter that prevents any logging other than from my own packages. Thanks.
– Eng.Fouad
Nov 14 '18 at 14:13
@Eng.Fouad glad to help!
– Aleksandr Semyannikov
Nov 14 '18 at 14:14
add a comment |
Consider that code:
Arrays.stream(new String{"ABC"}).forEach(LOGGER::info);
Arrays.stream(new String{"ABC"}).forEach(s -> LOGGER.info(s));
The difference is in the first case source of message is:
java.util.Spliterators$ArraySpliterator
in the second:
Main
Probably your logger has some filter that does not let logging events from java.util.Spliterators$ArraySpliterator class.
Try to check result of LOGGER.getFilter()
Consider that code:
Arrays.stream(new String{"ABC"}).forEach(LOGGER::info);
Arrays.stream(new String{"ABC"}).forEach(s -> LOGGER.info(s));
The difference is in the first case source of message is:
java.util.Spliterators$ArraySpliterator
in the second:
Main
Probably your logger has some filter that does not let logging events from java.util.Spliterators$ArraySpliterator class.
Try to check result of LOGGER.getFilter()
edited Nov 18 '18 at 18:21
answered Nov 14 '18 at 14:09
Aleksandr SemyannikovAleksandr Semyannikov
591216
591216
You got it right. I have a filter that prevents any logging other than from my own packages. Thanks.
– Eng.Fouad
Nov 14 '18 at 14:13
@Eng.Fouad glad to help!
– Aleksandr Semyannikov
Nov 14 '18 at 14:14
add a comment |
You got it right. I have a filter that prevents any logging other than from my own packages. Thanks.
– Eng.Fouad
Nov 14 '18 at 14:13
@Eng.Fouad glad to help!
– Aleksandr Semyannikov
Nov 14 '18 at 14:14
You got it right. I have a filter that prevents any logging other than from my own packages. Thanks.
– Eng.Fouad
Nov 14 '18 at 14:13
You got it right. I have a filter that prevents any logging other than from my own packages. Thanks.
– Eng.Fouad
Nov 14 '18 at 14:13
@Eng.Fouad glad to help!
– Aleksandr Semyannikov
Nov 14 '18 at 14:14
@Eng.Fouad glad to help!
– Aleksandr Semyannikov
Nov 14 '18 at 14:14
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%2f53301594%2floggerinfo-is-not-consumed-as-s-logger-infos%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
Interesting... it's working for me well. What version of java did you use?
– Aleksandr Semyannikov
Nov 14 '18 at 13:51
I tried it on both Java 8 and Java 11. Currently, I am using Oracle JDK 11.0.1.
– Eng.Fouad
Nov 14 '18 at 13:52
Works for me on java 8u191
– Julio Daniel Reyes
Nov 14 '18 at 13:54
Arrays.stream(new String { "ABC", "DEF", "XYZ" }).forEach(LOGGER::info);works fine for me (Java 8), LOGGER being ajava.util.logging.Logger.– Alex Shesterov
Nov 14 '18 at 13:54