Fine level messages aren't showing - java

My .FINE log messages aren't showing in the output.
public void playerCoinUpdate(Player player, Coin coin, GameEngine engine)
{
String update = String.format(player.getPlayerName() +" coin " + coin.getNumber() + "flipped to " + coin.getFace() + "\n");
// intermediate results logged at Level.FINE
logger.log(Level.FINE, update);
}
It should output a message to the console.

The message will not show if your logger level is higher than the message level. This can be fixed by changing the logger level:
logger.setLevel(level.FINE);
This will now log messages that are level FINE and above. So levels FINER and FINEST will be ignored. Alternatively, you could allow all messages to be logged by using this:
logger.setLevel(level.ALL);
The Java doc has other useful information regarding logging.

Related

JDA Discord Bot not recognizing user messages properly

Whenever a user in my discord sends a message, I try to log that message, however it thinks the message length is always zero. The one exception to this is that the bots own messages it detects perfectly fine.
#Override
public void onMessageReceived(#NotNull MessageReceivedEvent event) {
String message = event.getMessage().getContentRaw();
System.out.println(message + " - " + message.length());
if (!event.getAuthor().isBot()) {
event.getMessage().reply("test").queue();
}
}
The output of the above code in the terminal is as follows when I send a message of "hello":
- 0
test - 4
As it can be seen, the contents and length of my "hello" message is not being recognized, but my bots reply of "test" is recognized perfectly fine. I am quite puzzled by this issue and any help would be appreciated.
You are missing the message content intent. This is explained in the troubleshooting guide.

Get Browser logs and make TestNG test fail if there are any SEVERE logs from browser console

I'm Designing an automation framework using Selenium+TestNG+Java.
I want to check browser logs, if there are any SEVERE logs while executing any tests, it should mark the test as FAIL.
I'm able to identify the way to fetch the browser logs and able to add it in my internal logger.
public void analyzeBrowserLogs() {
String type = "browser";
List<LogEntry> entries = driver.manage().logs().get(type).getAll();
System.out.println(entries.size() + " " + type + " log entries found");
for (LogEntry entry : entries) {
//if(entry.getMessage().contains("SEVERE"))
System.out.println(new Date(entry.getTimestamp()) + " " + entry.getLevel() + " " + entry.getMessage());
}
}
I got stuck how could I check logs continually while executing the TestCases and If any SEVERE error occurs in logs, I need to mark the corresponding test status as FAIL.
It would be great addition to my framework if I'm able to design this logger feature.
Continuously polling the logs while the browser is basically running your test would not only be an overkill, but it may also cause flaky results.
So its not advisable to have your poll logs from browser, while test is running, happen in parallel.
What you should be doing is:
Leverage EventFiringWebDriver (see javadocs here) as your WebDriver implementation instead of RemoteWebDriver when your framework instantiates webdriver.
Plug in a custom implementation of WebDriverEventListener (see javadocs here) wherein you would identify events that cause javascript to get executed (usually its when a new page loads ) and for those events, within its afterXXX method, you make a call to your analyzeBrowserLogs() and if there's a SEVERE error, you throw an exception, which causes TestNG to automatically fail your test

Using Logger in Domino Java Agent

I want to get away from using a string buffer and System.out.println in Java agents.
I want to use java.util.Logger.
When I log using
logger.info("logger started")
output in the Domino Server Console
[0FDC:000D-1258] 06/07/2018 03:13:57 PM Agent Manager: Agent error: Jun 07, 2018 3:13:57 PM TransferDocsToServerNSF NotesMain INFO: logger started.
Notice the Domino platform added "Agent error" before the output when using Level.INFO
I then tried using .logp with Level.FINE and no output shows up.
logger.setLevel(Level.ALL); or logger.setLevel(Level.FINE);
logger.logp(Level.FINE,this.getClass().getSimpleName(),"main","logger started.");
output: No output. Anything lower than Level.INFO does not print.
How can I target the Domino logging to show Level.FINE?
And, what can I do for INFO for Domino not to consider all Level.INFO to be errors?
I was able to print logging at different levels after editing this file on the server:
(server install) IBM/Domino/jvm/lib/logging.properties
# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers. For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= FINEST
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = FINEST
Testing Java code
logger = Logger.getLogger("com.xpagesbeast");
logger.setUseParentHandlers(false); //do not use the global logger (avoid two outputs per log)
create a custom handler if one does not exist, watch our here, you can create more than one the JVM will track.
if(logger.getHandlers().length == 0){
System.out.println("Adding a new handler");
consoleHandler = new ConsoleHandler();
logger.addHandler(consoleHandler);
}
logger.getHandlers()[0].setLevel(Level.INFO);
logger.severe("test severe logging " + logger.getName());
logger.warning("test warning logging " + logger.getName());
logger.info("test info logging " + logger.getName());
logger.finer("test finer logging " + logger.getName());
logger.finest("test finest logging " + logger.getName());
Now I can set a debug flag in a Database or environment variable and assign the logging level as detailed as I want.
For example, if I want to debug the application, I can set a value in a database that is read by the application that determines what logging level that handler will use.

java.util.logging.Logger prints the Message twice in console

I am running a jar file in a console and I put logger so I can trace the if there are any errors. However, the message info prints twice in the console. Can I prevent this?
My code:
public static void main(String[] args) {
Logger log = Logger.getLogger("Logger");
log.setLevel(Level.ALL);
ConsoleHandler handler = new ConsoleHandler();
handler.setFormatter(new SimpleFormatter());
handler.setLevel(Level.ALL);
log.addHandler(handler);
log.log(Level.INFO, "Reading Configuration File");
}
Console:
Jul 22, 2015 9:30:33 PM com.ouc.mv90.conversion.CSVtoMV90Converter main
INFO: Reading Configuration File
Jul 22, 2015 9:30:33 PM com.ouc.mv90.conversion.CSVtoMV90Converter main
INFO: Reading Configuration File
Get ready for a facepalm. This question is basically a duplicate of this SO question, but I am giving an answer anyway.
What is happening is that your Logger class already has a default handler which prints to the System.out console. I expect that just the following code will generate output in your console:
Logger log = Logger.getLogger("Logger");
log.setLevel(Level.ALL);
log.log(Level.INFO, "Reading Configuration File");
But you have gone above and beyond this by adding a second handler which also is directed towards the console. Remove this second handler and that should take care of duplicate messages.
I have run into this problem and in my case the solution was:
Logger logger = Logger.getLogger("Logger");
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.ALL);
logger.addHandler(consoleHandler);
logger.setLevel(Level.ALL);
logger.setUseParentHandlers(false);
Notice the last line that calls to setUseParentHandlers. Without this line I get duplicate logs. I have checked at run time that in my case the number of handlers returned by logger.getHandlers() is just 1 after adding my console handler.
If I try
Logger logger = Logger.getLogger("Logger");
logger.setLevel(Level.ALL);
logger.log(Level.INFO, "Reading Configuration File");
logger.log(Level.FINE, "another message");
in this case I don't get duplicate logs but I don't get anything finer than INFO level either.
Just remove all handlers before add it to your logger, like :
for (Handler handler : logger.getHandlers()) { logger.removeHandler(handler);}
public static void main(String[] args) {
Logger log = Logger.getLogger("Logger");
log.setLevel(Level.ALL);
log.log(Level.INFO, "Reading Configuration File");
}
The above code itself is sufficient to print your log once with it's default log handler. When you add handlers, it will only print in the console using it's default handler, the logging is also directed to your all added handlers to print. Since you are adding another console handler to your logger, veerything will get printed (logged on the console) twice.

Is it possible to log only one level messages with Log4J

If I set the log level to DEBUG, All messages with log level >= DEBUG will be logged/printed.
But can I set log level to only DEBUG, such that messages with log level only with DEBUG will be printed. Or can give a range like print all messages whose log level is >=DEBUG but < ERROR?
Maybe you can use a LevelMatchFilter?
At some situation, You have to write logs to different outputs according to the level. how can it be done by simply configuration of Log4j? There are some methods below.
http://wiki.apache.org/logging-log4j/LogToAppenderByLevel
As said Jarle you have to use LevelMatchFilter.
I will demonstrate it with one simple exam:
log4j.rootLogger = WARN, admin
log4j.appender.admin=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.admin.rollingPolicy = org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.admin.rollingPolicy.FileNamePattern = Files\\TestLevels-%d{dd-MM-yyy}.txt
log4j.appender.admin.layout = org.apache.log4j.PatternLayout
log4j.appender.admin.layout.ConversionPattern = Date: %d{dd-MM-yyyy} Time: %d{HH:mm:ss} Message [%m]%n
log4j.appender.admin.filter.01=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.admin.filter.01.LevelToMatch=FATAL
log4j.appender.admin.filter.01.AcceptOnMatch=false
log4j.appender.admin.filter.02=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.admin.filter.02.LevelToMatch=ERROR
log4j.appender.admin.filter.02.AcceptOnMatch=true
log4j.appender.admin.filter.03=org.apache.log4j.varia.LevelMatchFilter
log4j.appender.admin.filter.03.LevelToMatch=WARN
log4j.appender.admin.filter.03.AcceptOnMatch=false
In my source I append only ERROR messages to file with name TestLevels.txt

Categories

Resources