Java 17 Recipes


Note the  java.util.logging framework is configured by the properties  log file. 9-9. Rotating and Purging Logs


Download 3.2 Mb.
Pdf ko'rish
bet178/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   174   175   176   177   178   179   180   181   ...   245
Bog'liq
Java 17 Recipes

Note the 
java.util.logging framework is configured by the properties 
log file.
9-9. Rotating and Purging Logs
 Problem
You have started to log information, but the information logged continues growing out 
of control. You would like to keep only the last 250 KB worth of log entries within your 
log files.
 Solution
Use java.util.logging to configure rolling logs. In this example, a logger named 
recipeLogger logs many messages. The output produces rolled log files with the most 
recent logged information in the important Log0.log file.
public static void main (String[] args) throws IOException {
Recipe9_9 recipe = new Recipe9_9();
recipe.start();
}
private void start() {
loadLoggingConfiguration();
Logger logger = LoggerFactory.getLogger("recipeLogger");
logger.info("Logging for the first Time!");
logger.warn("A warning to be had");
logger.severe("This is an error!");
Chapter 9 exCeptions and Logging


348
Logger rollingLogger = Logger.getLogger("rollingLogger");
for (int i =0;i < 5000;i++) {
rollingLogger.info("Logging for an event with :"+i);
}
}
private void loadLoggingConfiguration() {
FileInputStream ins = null;
try {
ins = new FileInputStream(new File("logging.properties"));
LogManager.getLogManager().readConfiguration(ins);
} catch (IOException e) {
e.printStackTrace();
}
}
The following is the logging.properties file in the main folder of the project.
handlers = java.util.logging.FileHandler
recipeLogger.level=INFO
.level=ALL
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern=LogApplication%g.log
java.util.logging.FileHandler.limit=50000
java.util.logging.FileHandler.count=4

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   174   175   176   177   178   179   180   181   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling