I wrote a simple program to execute a particularly large SQL file on a mysql database . The program is working fine, but because the SQL has large amount of data , the log generated is very large and unreadable. Is there some way to make jdbc print only errors or make it write the log into a file .
-
In catch() block, you can create if conditions and accordingly you can insert those logs wherever you wantsForSujit– sForSujit2017-08-10 07:31:32 +00:00Commented Aug 10, 2017 at 7:31
-
read this stackoverflow.com/questions/15758685/…Youcef LAIDANI– Youcef LAIDANI2017-08-10 07:32:06 +00:00Commented Aug 10, 2017 at 7:32
-
How do you generate the log currently? Normally, you use a logging framework for this kind of stuff, which allows you to simply configure which logging messages go where.Florian Schaetz– Florian Schaetz2017-08-10 07:32:08 +00:00Commented Aug 10, 2017 at 7:32
-
are you using log4j?sForSujit– sForSujit2017-08-10 07:34:06 +00:00Commented Aug 10, 2017 at 7:34
-
@sForSujit no I am not using log4j or anyother logger . All logs are written to console .born– born2017-08-10 08:14:28 +00:00Commented Aug 10, 2017 at 8:14
Add a comment
|
1 Answer
You can use any Logger, like Log4j and log in Catch statement:
Steps to follow:
Download latest log4j distribution.
Add log4j’s jar library into your program’s classpath.
Create log4j’s configuration.
Initialize log4j with the configuration.
Create a logger.
Put logging statements into your code.
Logger logger = Logger.getLogger(MyClass.class);
try {
// Your risky code goes between these curly braces!!!
}
catch(Exception ex) {
// Log your errors here
logger.info("This is my first log4j's statement");
}
finally {
// Your must-always-be-executed code goes between these
// curly braces. Like closing database connection.
}
`
Ref:http://www.codejava.net/coding/how-to-configure-log4j-as-logging-mechanism-in-java
3 Comments
born
Ya I think log4j is the way to go . I'll give it a try .
mohit sharma
@born let me know you exact need so that i can modify code accordingly , meanwhile this will give you a fair idea about logging SQL errors
born
Sorry for replying late . Followed your Idea . Got it working thanks . @mohit sharma