hi i'm working with a netbeans project, that they gave me to work on, and i need to check if the connection to the database (postgres) are working well, and i notice that there are lines in the code like
static Logger log = Logger.getLogger(getNivel.class);
then
if (user != null) {
log.debug("Usuario identificado: " + user.getIdUsuario() + "obteniendo su cuadro de mandos");
but i don't know how to see if the connection is actually working, because i can't find the log file. so i searched the internet and i found this page
link
but i don't really understand what i should do to see those messages. Can anybody help me?
Not sure which logging library you use, but I presume you use the standard java logging api (java.util.logging package) ...
You dont probably see it because you are using DEBUG level of logging and your project is set to print WARNING and above I believe. So if you what to quickly see whats going on, either change temporarily the code to:
if (user != null) {
log.severe("Usuario identificado: " + user.getIdUsuario() + "obteniendo su cuadro de mandos");
OR create logging.properties file in the root of you project, where you will enable logging on DEBUG level for the class or package for which you want to see the logs:
com.mycompany.mypackgepath.myclass.level=DEBUG
Note, that there might be already such a file in your project. Also you can add more handlers in order to print the log output to a file as well as to netbeans console
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.FileHandler.pattern = %h/Library/Logs/xd%g-%u.log
java.util.logging.FileHandler.limit = 10485760
java.util.logging.FileHandler.count = 2
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.FileHandler.encoding = UTF8
You might want to check this tutorial for example: http://www.javapractices.com/topic/TopicAction.do?Id=143
Btw. the logger should be private and final ;)
Related
I need create a lot of .log file in different folders from one java method.
Example:
home/work/folder1/log1.log
home/work/folder1/log2.log
home/work/folder2/log3.log
...............................
In java method i create dynamic Logger:
private Logger getLogger(String extId, String workId) {
String postfix = String.join(
".",
getClass().getName(),
extId, //folder1, folder2
String.valueOf(workId) //log1, log2
);
return LoggerFactory.getLogger(postfix);
}
How i can configurate logback to runtime create files?
I see ch.qos.logback.classic.spi.Configurator, but don't inderstand what do next.
Thank for any helps!
Why do you care about your log folders?
You can log in one folder and use the elk-stack to verify them.
I always do it with one sperate folder per log level and configure my logpattern with log4j2. Logstash reads the logs immediatly when new ones are available und pushes them to elastic search. With kibana you can do many cool dashboards to analyze them.
First of all, Java is not my usual language, so I'm quite basic at it. I need to use it for this particular project, so please be patient, and if I have omitted any relevant information, please ask for it, I will be happy to provide it.
I have been able to implement coreNLP, and, seemingly, have it working right, but is generating lots of messages like:
ene 20, 2017 10:38:42 AM edu.stanford.nlp.process.PTBLexer next
ADVERTENCIA: Untokenizable: 【 (U+3010, decimal: 12304)
After some research (documentation, google, other threads here), I think (sorry, I don't know how I can tell for sure) coreNLP is finding the slf4j-api.jar in my classpath, and logging through it.
Which properties of the JVM can I use to set logging level of the messages that will be printed out?
Also, in which .properties file I could set them? (I already have a commons-logging.properties, a simplelog.properties and a StanfordCoreNLP.properties in my project's resource folder to set properties for other packages).
Om’s answer is good, but two other possibly useful approaches:
If it is just these warnings from the tokenizer that are annoying you, you can (in code or in StanfordCoreNLP.properties) set a property so they disappear: props.setProperty("tokenize.options", "untokenizable=NoneKeep");.
If slf4j is on the classpath, then, by default, our own Redwoods logger will indeed log through slf4j. So, you can also set the logging level using slf4j.
If I understand your problem, you want to disable all StanfordNLP logging message while the program is executing.
You can disable the logging message. Redwood logging framework is used as logging framework in Stanford NLP. First, clear the Redwood's default configuration(to display log message) then create StanfordNLP pipeline.
import edu.stanford.nlp.util.logging.RedwoodConfiguration;
RedwoodConfiguration.current().clear().apply();
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
Hope it helps.
In accordance with Christopher Manning's suggestion, I followed this link
How to configure slf4j-simple
I created a file src/simplelogger.properties with the line org.slf4j.simpleLogger.defaultLogLevel=warn.
I am able to solve it by setting a blank output stream to system error stream.
System.setErr(new PrintStream(new BlankOutputStream())); // set blank error stream
// ... Add annotators ...
System.setErr(System.err); // Reset to default
Accompanying class is
public class BlankOutputStream extends OutputStream {
#Override
public void write(int b) throws IOException {
// Do nothing
}
}
Om's answer disables all logging. However, if you wish to still log errors then use:
RedwoodConfiguration.errorLevel().apply();
I also use jdk logging instead of slf4j logging to avoid loading slfj dependencies as follows:
RedwoodConfiguration.javaUtilLogging().apply();
Both options can be used together and in any order. Required import is:
import edu.stanford.nlp.util.logging.RedwoodConfiguration;
I am running into logging issues after upgrading my Java EE app from Weblogic/JDeveloper to 12.1.2 to 12.1.3.
System.out.println is printing to the server log fine but log.info("test") is not. The logging works if I set the log level e.g. log.setLevel(Level.INFO). Here are my test results.
// This works
System.out.println("test1");
// Output when run: test1
// This does not work. Nothing prints to the server log
log.info("test2");
// The above works if I set the log level
log.setLevel(Level.INFO);
log.info("test3");
// Output when run: test3
// This prints null. It appears that logging level is set to null on server startup
System.out.println(" what is my current logging level: " + log.getLevel());
As per Oracle documentation if no logging configuration is provided then the default logging.properties in JDK/JRE/Lib is used. The default log level is INFO.
I have also tried to load up logging.properties and switch log4j but nothing works.
I don't want to set log level to something in every class. Is there a way to set this on server startup or debug what is causing/setting the logging level to null.
From the logger documentation:
The result may be null, which means that this logger's effective level will be inherited from its parent.
So you only have to set the level of the root logger. You should check that a ConsoleHandler is installed and that the level is set to INFO or lower. You should also check that writing to System.err shows up in the log file as that is the stream that is used for the ConsoleHandler.
I use this code for mine "Logg class" so every class is connected with it, so every exception from every class use this. if you have many classes you want to get logged from this is kind a smart and easy way. I just have one problem too, this code creates a txt file every time you run a program and it's connected with the Logg class. I will fix it soon, so i will edit this answer then. heres my code:
Top of class
private final static Logger logger = Logger.getLogger(Logg.class.getName());
private static FileHandler fh = null;
and in the method you can have this;
Properties prop = new Properties();
InputStream in = Logg.class.getResourceAsStream("loggconfig");
if (in != null) {
prop.load(in);
} else {
throw new FileNotFoundException("property file '" + in + "' not found in the classpath");
}
Some exemple, this txt file will be named the current year, date and time.
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
fh = new FileHandler((dateFormat.format(date) + ".log"), false);
Logger l = Logger.getLogger("");
fh.setFormatter(new SimpleFormatter());
l.addHandler(fh);
l.setLevel(Level.INFO);
have this under argument you want to logg:
logger.log(Level.WARNING, "Bla bla");
you can also have a file, like a config so you can choose what level you want to get logged. Then you need to create a property file, my is named loggconfig.
in the config file i have this code:
Level=WARNING
so it just every exception i have as a WARNING, i can change it to INFO to so i get everything logged. Smart to only use two levels in the code.
So if you want to use config file u need to change l.setLevel too this:
l.setLevel(Level.parse(prop.getProperty("Level")));
so it takes the level from config file.
Some logging levels appear to be broke?
I run a Java web start (which I will begin to call JWS from now on) application straight from a GlassFish 3.1.2.2 instance. The client has a static logger like so:
private final static Logger LOGGER;
static {
LOGGER = Logger.getLogger(App.class.getName());
// Not sure how one externalize this setting or even if we want to:
LOGGER.setLevel(Level.FINER);
}
In the main method, I begin my logic with some simple testing of the logging feature:
alert("isLoggable() INFO? " + LOGGER.isLoggable(Level.INFO)); // Prints TRUE!
alert("isLoggable() FINE? " + LOGGER.isLoggable(Level.FINE)); // ..TRUE
alert("isLoggable() FINER? " + LOGGER.isLoggable(Level.FINER)); // ..TRUE
alert("isLoggable() FINEST? " + LOGGER.isLoggable(Level.FINEST)); // ..FALSE
My alert methods will display a JOptionPane dialog box for "true GUI logging". Anyways, you see the printouts in my comments I added to the code snippet. As expected, the logger is enabled for levels INFO, FINE and FINER but not FINEST.
After my alert methods, I type:
// Info
LOGGER.info("Level.INFO");
LOGGER.log(Level.INFO, "Level.INFO");
// Fine
LOGGER.fine("Level.FINE");
LOGGER.log(Level.FINE, "Level.FINE");
// Finer
LOGGER.finer("Level.FINER");
LOGGER.log(Level.FINER, "Level.FINER");
LOGGER.entering("", "Level.FINER", args); // <-- Uses Level.FINER!
// Finest
LOGGER.finest("Level.FINEST");
LOGGER.log(Level.FINEST, "Level.FINEST");
I go to my Java console and click on the tab "Advanced", then I tick "Enable logging". Okay let's start the application. Guess what happens? Only Level.INFO prints! Here's my proof (look at the bottom):
I've done my best to google for log files on my computer and see if not Level.FINE and Level.FINER end up somewhere on the file system. However, I cannot find the log messages anywhere.
Summary of Questions
Why does it appear that logging of Level.FINE and Level.FINER does not work in the example provided?
I set the logging level in my static initializing block, but I'd sure like to externalize this setting to a configuration file of some sort, perhaps packaged together with the EAR file I deploy on GlassFish. Or why not manually write in some property in the JNLP file we download from the server. Is this possible somehow?
Solution for problem no 1.
After doing a little bit more reading on the topic, I concluded that a logger in Java uses a handler to publish his logs. And this handler in his turn has his own set of "walls" for what levels he handles. But this handler need not be attached directly to our logger! You see loggers are organized in a hierarchical namespace and a child logger may inherit his parents handlers. If so, then By default a Logger will log any output messages to its parent's handlers, and so on recursively up the tree (see Java Logging Overview - Oracle).
I ain't saying I get the full picture just yet, and I sure didn't find any quotes about how all of this relates to a Java Web Start application. Surely there has to be some differences. Anyways, I did manage to write together this static initializing block that solves my immediate problem:
static {
LOGGER = Logger.getLogger(App.class.getName());
/*
* This logic can be externalized. See the next solution!
*/
// DEPRECATED: LOGGER.setLevel(Level.FINER);
if (LOGGER.getUseParentHandlers())
LOGGER.getParent().getHandlers()[0].setLevel(Level.FINER);
else
LOGGER.setLevel(Level.FINER);
}
Solution for problem no 2.
The LogManager API docs provided much needed information for the following solution. In a subdirectory of your JRE installation, there is a subdirectory called "lib" and in there you shall find a "logging.properties" file. This is the full path to my file on my Windows machine:
C:\Program Files (x86)\Java\jre7\lib\logging.properties
In here you can change a lot of flavors. One cool thing you could do is to change the global logging level. In my file, this was done on row 29 (why do we see only a dot in front of the "level"? The root-parent of all loggers is called ""!). That will produce a hole lot of output; on my machine I received about one thousand log messages per second. Thus changing the global level isn't even plausible enough to be considered an option. Instead, add a new row where you specify the level of your logger. In my case, I added this row:
martinandersson.com.malivechat.app.App.level = FINER
However, chances are you still won't see any results. In solution no 1, I talked about how loggers are connected to handlers. The default handler is specified in logging.properties, most likely on row 18. Here's how my line reads:
handlers= java.util.logging.ConsoleHandler
Also previously, I talked about how these handlers in their turn use levels for what should trouble their mind. So, find the line that reads something like this (should now be on row 44?):
java.util.logging.ConsoleHandler.level = INFO
..and in my case I swapped "INFO" to "FINER". Problem solved.
But!
My original inquiry into this matter still hasn't provided an answer how one can set these properties closer in par with the application deployment. More specifically, I would like to attach these properties in a separate file, bundled with the application EAR file I deploy on GlassFish or something like that. Do you have more information? Please share!
I'm using java logging to write log messages of my application to a log file and other destinations. Having set the log level to FINE, I also get (unwanted) messages from AWT/Swing, such as:
{0}, when grabbed {1}, contains {2}
and others. Looking at the JDK source code (see e.g. here), one sees that the name of the corresponding logger is sun.awt.X11.grab.XWindowPeer.
What I understood from the Java logging framework is that this logging handler should inherit its loglevel from its parents like sun.awt.
I tried the following:
Logger.getLogger("sun.awt").setLevel(Level.OFF);
but the AWT/Swing debug messages still appear in the log output.
What's the recommended way of programmatically disabling these log messages (while still allowing FINE messages from other sources) ?
If you just want to log the messages of your own application, you can disable all messages and then explicitly enable messages for your application:
Logger.getRootLogger().setLevel(Level.OFF);
Logger.getLogger("package.of.your.application").setLevel(Level.ALL);
Inside the properties-file for logging (e.g. logging.properties) this would be:
.level = OFF
package.of.your.application.level = ALL
I had the same problem today. Searching on google this is the top url and I don't find a good source to a answer, so I will post mine :)
Assuming that Andre use the java.util.logging API, it's possible to add a Handler that will control the format of your log, using setFormatter(Formatter newFormatter).
So I extended Formatter and checked if the class of the log contains java.awt, javax.swing or sun.awt.
class MyFormatLog extends Formatter {
#Override
public String format(LogRecord record) {
if( !record.getSourceClassName().contains("java.awt") &&
!record.getSourceClassName().contains("javax.swing.") &&
!record.getSourceClassName().contains("sun.awt.") ) {
//format my output...
} else {
return "";
}
}
}
I could not find the getRootLogger() method in the Logger class any more. This is what works for me:
logger = Logger.getLogger("my.package");
Logger l = logger;
while (l != null) {
l.setLevel(Level.OFF);
l = l.getParent();
}
logger.setLevel(Level.FINER);
Logger.getLogger("java.awt").setLevel(Level.OFF);
Logger.getLogger("sun.awt").setLevel(Level.OFF);
Logger.getLogger("javax.swing").setLevel(Level.OFF);
Try
Logger.getRootLogger().setLevel(Level.OFF);