NcLog.java 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package u8c.log;
  2. import nc.bs.framework.common.RuntimeEnv;
  3. import org.apache.log4j.Logger;
  4. import java.io.IOException;
  5. import java.io.BufferedWriter;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.OutputStreamWriter;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. /**
  12. * nclog
  13. * @author YY
  14. * @datetime 2021-9-29
  15. */
  16. public class NcLog
  17. {
  18. private static final Logger logger = Logger.getLogger(NcLog.class);
  19. private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  20. private static SimpleDateFormat format$ = new SimpleDateFormat("yyyy-MM-dd");
  21. public static void info(String str)
  22. {
  23. String logFilePath = getLogFile();
  24. File file = new File(logFilePath);
  25. BufferedWriter fos = null;
  26. try
  27. {
  28. fos = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
  29. str = format.format(new Date()) + " " + str + "\r\n";
  30. fos.write(str + "\r\n");
  31. fos.flush();
  32. }
  33. catch (Exception localException)
  34. {
  35. if (fos != null)
  36. try
  37. {
  38. fos.close();
  39. }
  40. catch (IOException localIOException)
  41. {
  42. }
  43. }
  44. finally {
  45. if (fos != null)
  46. try
  47. {
  48. fos.close();
  49. }
  50. catch (IOException localIOException2)
  51. {
  52. }
  53. }
  54. }
  55. private static String getLogFile() {
  56. String destFileFolder = RuntimeEnv.getInstance().getNCHome() + File.separator + "mancLog" + File.separator + format$.format(new Date());
  57. String destFilePath = destFileFolder + File.separator + "org.log";
  58. File destFolder = new File(destFileFolder);
  59. if (!destFolder.exists()) {
  60. destFolder.mkdirs();
  61. }
  62. File destFile = new File(destFilePath);
  63. if (!destFile.exists()) {
  64. try
  65. {
  66. destFile.createNewFile();
  67. }
  68. catch (Exception e)
  69. {
  70. logger.info(e);
  71. }
  72. }
  73. return destFilePath;
  74. }
  75. }