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 .
Asked
Active
Viewed 292 times
0
-
In catch() block, you can create if conditions and accordingly you can insert those logs wherever you want – sForSujit Aug 10 '17 at 07:31
-
read this https://stackoverflow.com/questions/15758685/how-to-write-logs-in-text-file-when-using-java-util-logging-logger – Youcef LAIDANI Aug 10 '17 at 07: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 Aug 10 '17 at 07:32
-
are you using log4j? – sForSujit Aug 10 '17 at 07:34
-
@sForSujit no I am not using log4j or anyother logger . All logs are written to console . – born Aug 10 '17 at 08:14
1 Answers
2
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

mohit sharma
- 1,050
- 10
- 20
-
-
@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 – mohit sharma Aug 10 '17 at 08:36
-
Sorry for replying late . Followed your Idea . Got it working thanks . @mohit sharma – born Sep 11 '17 at 10:25