PhmsLogger.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "PhmsLogger.h"
  2. #include "Util.h"
  3. #include "AllObjectManagerUtil.h"
  4. #include "Poco/Logger.h"
  5. #include "Poco/FileChannel.h"
  6. #include "Poco/Message.h"
  7. #include "Poco/AutoPtr.h"
  8. #include "Poco/File.h"
  9. #include "Poco/DateTime.h"
  10. #include "Poco/DateTimeFormatter.h"
  11. #include "Poco/Timestamp.h"
  12. #include "Poco/Timezone.h"
  13. #include "Poco/Timespan.h"
  14. #include "Poco/Exception.h"
  15. #include <stdio.h>
  16. #include <sstream>
  17. #include <iostream>
  18. using Poco::Logger;
  19. using Poco::FileChannel;
  20. using Poco::AutoPtr;
  21. using Poco::File;
  22. using Poco::Message;
  23. using Poco::Timestamp;
  24. using Poco::DateTime;
  25. using Poco::DateTimeFormatter;
  26. using Poco::Timestamp;
  27. using Poco::Timezone;
  28. using Poco::Timespan;
  29. using Poco::Exception;
  30. using std::ostringstream;
  31. CPhmsLogger* CPhmsLogger::m_pPhmsLogger = NULL;
  32. Mutex CPhmsLogger::m_mutex;
  33. CPhmsLogger::CPhmsLogger(void)
  34. {
  35. try
  36. {
  37. string stringWorkingDir = CUtil::GetCurrentAppDir();
  38. File fileDir(stringWorkingDir+"log/NET_LOG/", "gbk");
  39. fileDir.createDirectories();
  40. File fileLog(stringWorkingDir+"log/NET_LOG/log_phms_netlib.log", "gbk");
  41. fileLog.createFile();
  42. AutoPtr<FileChannel> pFileChannel = new FileChannel(fileLog.path());
  43. #if defined(POCO_OS_FAMILY_UNIX)
  44. pFileChannel->setProperty(FileChannel::PROP_ROTATION, "1 M");
  45. #else
  46. pFileChannel->setProperty(FileChannel::PROP_ROTATION, "1 days");
  47. #endif
  48. pFileChannel->setProperty(FileChannel::PROP_ARCHIVE, "timestamp");
  49. pFileChannel->setProperty(FileChannel::PROP_COMPRESS, "false");
  50. #if defined(POCO_OS_FAMILY_UNIX)
  51. pFileChannel->setProperty(FileChannel::PROP_PURGECOUNT, "2");
  52. #else
  53. pFileChannel->setProperty(FileChannel::PROP_PURGECOUNT, "30");
  54. #endif
  55. pFileChannel->setProperty(FileChannel::PROP_TIMES, "utc");
  56. Logger& logPhms = Logger::get("PHMS.NETLIB");
  57. logPhms.setChannel(pFileChannel);
  58. logPhms.setLevel(Message::PRIO_ERROR);
  59. }
  60. catch(Exception& e)
  61. {
  62. std::cout<<"PhmsHttp:"<<e.what()<<" File:"<<__FILE__<<" Line:"<<__LINE__<<std::endl;
  63. std::cout<<"PhmsHttp:"<<CUtil::GetCurrentAppDir()<<" File:"<<__FILE__<<" Line:"<<__LINE__<<std::endl;
  64. }
  65. catch(...)
  66. {
  67. }
  68. }
  69. CPhmsLogger::~CPhmsLogger(void)
  70. {
  71. Logger::shutdown();
  72. }
  73. CPhmsLogger* CPhmsLogger::GetPhmsLogger()
  74. {
  75. Mutex::ScopedLock lock(m_mutex);
  76. if(CPhmsLogger::m_pPhmsLogger == NULL)
  77. {
  78. m_pPhmsLogger = new CPhmsLogger;
  79. }
  80. return m_pPhmsLogger;
  81. }
  82. void CPhmsLogger::ReleasePhmsLogger()
  83. {
  84. //因为在CAllObjectManagerUtil::ReleasePhmsLogger中已经有锁控制了,所以此处不再用m_mutex控制
  85. if(m_pPhmsLogger != NULL)
  86. {
  87. delete m_pPhmsLogger;
  88. m_pPhmsLogger = NULL;
  89. }
  90. }
  91. void CPhmsLogger::WriteLog(string stringText, const char* stringFunction, const char* stringFile, int nLine)
  92. {
  93. try
  94. {
  95. Logger& logPhms = Logger::get("PHMS.NETLIB");
  96. DateTime dt;
  97. dt += Timespan(Timezone::utcOffset(), 0);
  98. string stringDateTime = DateTimeFormatter::format(dt, "%Y-%m-%d %H:%M:%S");
  99. string stringSourceFile = "source file: ";
  100. stringSourceFile += stringFile;
  101. ostringstream oStringStream;
  102. oStringStream<<"line: "<<nLine;
  103. string stringNLine = oStringStream.str();
  104. Message logMessage("PHMS.NETLIB", stringDateTime+"---"+stringText+"---"+stringFunction+"---"+stringSourceFile+"---"+stringNLine, Message::PRIO_ERROR);
  105. logPhms.log(logMessage);
  106. }
  107. catch(Exception&)
  108. {
  109. }
  110. }
  111. void CPhmsLogger::WriteLog(const Exception& e, const char* stringFunction, const char* stringFile, int nLine)
  112. {
  113. try
  114. {
  115. Logger& logPhms = Logger::get("PHMS.NETLIB");
  116. string stringException;
  117. stringException = e.displayText();
  118. DateTime dt;
  119. dt += Timespan(Timezone::utcOffset(), 0);
  120. string stringDateTime = DateTimeFormatter::format(dt, "%Y-%m-%d %H:%M:%S");
  121. string stringSourceFile = "source file: ";
  122. stringSourceFile += stringFile;
  123. ostringstream oStringStream;
  124. oStringStream<<"line: "<<nLine;
  125. string stringNLine = oStringStream.str();
  126. Message logMessage("PHMS.NETLIB", stringDateTime+"---"+stringException+"---"+stringFunction+"---"+stringSourceFile+"---"+stringNLine, Message::PRIO_ERROR);
  127. logPhms.log(logMessage);
  128. }
  129. catch(Exception&)
  130. {
  131. }
  132. }