Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.URL;
  3. import java.net.URLDecoder;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.Properties;
  7. import java.util.logging.Level;
  8. import java.util.logging.LogManager;
  9. import java.util.logging.Logger;
  10.  
  11.  
  12. /**
  13. * Класс настраивает Logger и LogManager. Формирует формат логов и место хранения. Все настройки хранит в файле logger.properties
  14. */
  15. public class LogConfigurator {
  16. private static final LogManager logManager = LogManager.getLogManager();
  17. private static final Logger LOGGER = Logger.getLogger(LogConfigurator.class.getName());
  18.  
  19. public static void configureLog() {
  20. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd'at'HH.mm.ss");
  21. File logDir = new File("Logs");
  22. if (!logDir.exists()) makeDir(logDir);
  23. File logFile = new File("Logs/log" + dateFormat.format(new Date()) + ".txt");
  24. Properties properties = new Properties();
  25.  
  26. try (FileInputStream fis = new FileInputStream("logger.properties")) {
  27. properties.load(fis);
  28. properties.setProperty("java.util.logging.FileHandler.pattern", logFile.getAbsolutePath());
  29. } catch (IOException e) {
  30. LOGGER.log(Level.SEVERE, "Error in loading configuration", e);
  31. }
  32.  
  33. try (FileOutputStream fos = new FileOutputStream("logger.properties")) {
  34. properties.store(fos, null);
  35. } catch (FileNotFoundException e) {
  36. LOGGER.log(Level.SEVERE, "Error in loading configuration", e);
  37. } catch (IOException e) {
  38. LOGGER.log(Level.SEVERE, "Error in loading configuration", e);
  39. }
  40.  
  41. try {
  42. logManager.readConfiguration(new FileInputStream("logger.properties"));
  43. } catch (IOException exception) {
  44. LOGGER.log(Level.SEVERE, "Error in loading configuration", exception);
  45. }
  46. }
  47.  
  48. /**
  49. * Метод проверяет есть ли в директории с исполняемым файлов каталог Logs для хранения логов. Если нет - создает каталог.
  50. */
  51. private static void makeDir(File logDir) {
  52. logDir.mkdirs();
  53. if (!logDir.exists()) {
  54. try {
  55. String path = getProgramPath();
  56. String fileSeparator = System.getProperty("file.separator");
  57. String newDir = path + fileSeparator + logDir.getName() + fileSeparator;
  58. File file = new File(newDir);
  59. file.mkdir();
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65.  
  66. /**
  67. * Метод возвращает путь к jar-файлу
  68. */
  69. private static String getProgramPath() throws UnsupportedEncodingException {
  70. URL url = MainFileHelper.class.getProtectionDomain().getCodeSource().getLocation();
  71. String jarPath = URLDecoder.decode(url.getFile(), "UTF-8");
  72. String parentPath = new File(jarPath).getParentFile().getPath();
  73. return parentPath;
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement