ULoggerForMyBean.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. unit ULoggerForMyBean;
  2. interface
  3. uses
  4. mybean.core.objects, Classes, SysUtils, StrUtils, WindowsSysVersion, superobject,
  5. Dialogs, IdHTTP, ULogger, InterfaceLogger, DateUtils, WinSock;
  6. type
  7. TLoggerBean = class(TMyBeanInterfacedObject, ILogger)
  8. private
  9. FLoginName: string;
  10. FIP: string;
  11. FPlatform: string;
  12. FEnable: Boolean;
  13. FUrl,
  14. FAppCode,
  15. FClientVersion: string;
  16. public
  17. procedure Log(AMessage, ACode, ALoginName: string); overload; stdcall;
  18. procedure Log(AMessage, ACode, ALoginName, AStrLevel: string); overload; stdcall;
  19. procedure AsynLog(AMessage, ACode, ALoginName: string); stdcall;
  20. procedure Success(AMessage, ACode, ALoginName: string); stdcall;
  21. procedure Info(AMessage, ACode, ALoginName: string); stdcall;
  22. procedure Debug(AMessage, ACode, ALoginName: string); stdcall;
  23. procedure Warning(AMessage, ACode, ALoginName: string); stdcall;
  24. procedure Error(AMessage, ACode, ALoginName: string); stdcall;
  25. procedure Init;
  26. procedure LoadConfig;
  27. constructor Create; override;
  28. end;
  29. implementation
  30. { TLoggerBean }
  31. procedure TLoggerBean.AsynLog(AMessage, ACode, ALoginName: string);
  32. var
  33. AJson: TStringStream;
  34. AHttp: TIdHTTP;
  35. Respone, AJsonStr: string;
  36. ATimestamp: Int64;
  37. begin
  38. if not FEnable then
  39. Exit;
  40. ATimestamp := (DateTimeToUnix(Now) - 8*60*60) * 1000;
  41. AJsonStr := Format(JSON_LOG, [FAppCode, ALoginName, ATimestamp, FPlatform, FClientVersion, FIP, ACode, AMessage, 'error']);
  42. AJsonStr := StringReplace(AJsonStr, '\r\n', '[\r][\n]', [rfReplaceAll]);
  43. AJsonStr := StringReplace(AJsonStr, #13#10, '\r\n', [rfReplaceAll]);
  44. AJson := TStringStream.Create(AnsiToUtf8(AJsonStr));
  45. AHttp := TIdHttp.Create(nil);
  46. AHttp.HandleRedirects := True;//允许头转向
  47. AHttp.ReadTimeout := 5000;//请求超时设置
  48. try
  49. AJson.Position := 0;
  50. Respone := AHttp.Post(FUrl, AJson);
  51. Respone := Utf8ToAnsi(Respone)
  52. finally
  53. AJson.Free;
  54. AHttp.Free;
  55. end;
  56. end;
  57. procedure TLoggerBean.Log(AMessage, ACode, ALoginName: string);
  58. begin
  59. Log(AMessage, ACode, ALoginName, 'error');
  60. end;
  61. procedure TLoggerBean.Success(AMessage, ACode, ALoginName: string);
  62. begin
  63. Log(AMessage, ACode, ALoginName, 'success');
  64. end;
  65. procedure TLoggerBean.Warning(AMessage, ACode, ALoginName: string);
  66. begin
  67. Log(AMessage, ACode, ALoginName, 'warning');
  68. end;
  69. procedure TLoggerBean.LoadConfig;
  70. var
  71. AFullPath: string;
  72. jo: ISuperObject;
  73. begin
  74. AFullPath := ExtractFilePath(ParamStr(0)) + CONFIG_DIR;
  75. if not FileExists(AFullPath) then
  76. begin
  77. FEnable := False;
  78. Exit;
  79. end;
  80. try
  81. jo := TSuperObject.ParseFile(AFullPath, False);
  82. FEnable := jo['enable'].AsBoolean();
  83. FUrl := jo['url'].AsString();
  84. FAppCode := jo['appCode'].AsString();
  85. FClientVersion := jo['clientVersion'].AsString();
  86. except
  87. on E: Exception do
  88. begin
  89. FEnable := False;
  90. Dialogs.ShowMessage(E.Message);
  91. end;
  92. end;
  93. end;
  94. procedure TLoggerBean.Log(AMessage, ACode, ALoginName, AStrLevel: string);
  95. var
  96. AJson: string;
  97. ASendThread: TSendLogThread;
  98. ATimestamp: Int64;
  99. begin
  100. if not FEnable then
  101. Exit;
  102. try
  103. ATimestamp := (DateTimeToUnix(Now) - 8*60*60) * 1000;
  104. AJson := Format(JSON_LOG, [FAppCode, ALoginName, ATimestamp, FPlatform, FClientVersion, FIP, ACode, AMessage, AStrLevel]);
  105. AJson := StringReplace(AJson, '\r\n', '[\r][\n]', [rfReplaceAll]);
  106. AJson := StringReplace(AJson, #13#10, '\r\n', [rfReplaceAll]);
  107. except
  108. LoadConfig;
  109. Exit;
  110. end;
  111. ASendThread := TSendLogThread.Create(FUrl, Ajson);
  112. end;
  113. constructor TLoggerBean.Create;
  114. begin
  115. inherited;
  116. LoadConfig;
  117. Init;
  118. end;
  119. procedure TLoggerBean.Debug(AMessage, ACode, ALoginName: string);
  120. begin
  121. Log(AMessage, ACode, ALoginName, 'debug');
  122. end;
  123. procedure TLoggerBean.Error(AMessage, ACode, ALoginName: string);
  124. begin
  125. Log(AMessage, ACode, ALoginName, 'error');
  126. end;
  127. procedure TLoggerBean.Info(AMessage, ACode, ALoginName: string);
  128. begin
  129. Log(AMessage, ACode, ALoginName, 'info');
  130. end;
  131. procedure TLoggerBean.Init;
  132. begin
  133. try
  134. FPlatform := '未知';
  135. FIP := '0.0.0.0';
  136. case GetWindowsSystemVersion of
  137. WinNone: FPlatform := '未知';
  138. Win95: FPlatform := 'Windows 95';
  139. Win98: FPlatform := 'Windows 98';
  140. WinMe: FPlatform := 'Windows Me';
  141. Win2000: FPlatform := 'Windows 2000';
  142. WinServer2000: FPlatform := 'Windows Server 2000';
  143. WinXp: FPlatform := 'Windows XP';
  144. WinXp64: FPlatform := 'Windows XP x64';
  145. WinServer2003: FPlatform := 'Windows Server 2003';
  146. WinHomeServer: FPlatform := 'Windows Home Server';
  147. WinServer2003R2: FPlatform := 'Windows Server 2003 R2';
  148. WinVista: FPlatform := 'Windows Vista';
  149. WinServer2008: FPlatform := 'Windows Server 2008';
  150. WinServer2008R2: FPlatform := 'Windows Server 2008 R2';
  151. Win7: FPlatform := 'Windows 7';
  152. end;
  153. FIP := LocalIP;
  154. except
  155. on E: Exception do
  156. begin
  157. Dialogs.ShowMessage(E.Message);
  158. end;
  159. end;
  160. end;
  161. end.