public class LibraryExample { public void processFile(String fileName) { try { // Code to process the file } catch (FileNotFoundException e) { // Handle the FileNotFoundException } catch (IOException e) { // Handle the IOException } catch (Exception e) { // Handle any other exceptions } } } import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class LibraryExample { private static final Logger logger = LogManager.getLogger(LibraryExample.class); public void processFile(String fileName) { logger.info("Processing file: {}", fileName); try { // Code to process the file logger.debug("File processed successfully"); } catch (FileNotFoundException e) { logger.error("File not found: {}", fileName); } catch (IOException e) { logger.error("IOException occurred: {}", e.getMessage()); } catch (Exception e) { logger.error("An error occurred during file processing: {}", e.getMessage()); } } } <?xml version="1.0" encoding="UTF-8"?> <Configuration> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] %logger{1.} - %msg%n" /> </Console> <File name="FileAppender" fileName="logs/application.log"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] %logger{1.} - %msg%n" /> </File> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console" /> <AppenderRef ref="FileAppender" /> </Root> </Loggers> </Configuration>


上一篇:
下一篇:
切换中文