├── .gitignore ├── LICENSE ├── README.md ├── examples ├── lazlogger │ ├── HelloWorld.lpi │ └── HelloWorld.lpr └── log4d │ ├── HelloWorld.lpi │ ├── HelloWorld.lpr │ └── LogConsoleAppender.pas └── src ├── main ├── LazLoggerLogger.pas ├── Log4DLogger.pas ├── NOPLogger.pas ├── SimpleLogger.pas ├── StringsLogger.pas ├── djLogAPI.pas ├── djLogOverLazLogger.pas ├── djLogOverLog4D.pas ├── djLogOverNOPLogger.pas ├── djLogOverSimpleLogger.pas └── djLoggerFactory.pas └── test ├── LazLoggerTests.pas ├── Log4DLoggerTests.pas ├── LoggerFactoryTests.pas ├── NOPLoggerTests.pas ├── SimpleLoggerTests.pas ├── StringsLoggerTests.pas ├── Unittests.dpr ├── Unittests.lpi └── Unittests.lpr /.gitignore: -------------------------------------------------------------------------------- 1 | # Uncomment these types if you want even more clean repository. But be careful. 2 | # It can make harm to an existing project source. Read explanations below. 3 | # 4 | # Resource files are binaries containing manifest, project icon and version info. 5 | # They can not be viewed as text or compared by diff-tools. Consider replacing them with .rc files. 6 | #*.res 7 | # 8 | # Type library file (binary). In old Delphi versions it should be stored. 9 | # Since Delphi 2009 it is produced from .ridl file and can safely be ignored. 10 | #*.tlb 11 | # 12 | # Diagram Portfolio file. Used by the diagram editor up to Delphi 7. 13 | # Uncomment this if you are not using diagrams or use newer Delphi version. 14 | #*.ddp 15 | # 16 | # Visual LiveBindings file. Added in Delphi XE2. 17 | # Uncomment this if you are not using LiveBindings Designer. 18 | #*.vlb 19 | # 20 | # Deployment Manager configuration file for your project. Added in Delphi XE2. 21 | # Uncomment this if it is not mobile development and you do not use remote debug feature. 22 | #*.deployproj 23 | # 24 | # C++ object files produced when C/C++ Output file generation is configured. 25 | # Uncomment this if you are not using external objects (zlib library for example). 26 | #*.obj 27 | # 28 | 29 | # Delphi compiler-generated binaries (safe to delete) 30 | *.exe 31 | *.dll 32 | *.bpl 33 | *.bpi 34 | *.dcp 35 | *.so 36 | *.apk 37 | *.drc 38 | *.map 39 | *.dres 40 | *.rsm 41 | *.tds 42 | *.dcu 43 | *.lib 44 | *.a 45 | *.o 46 | *.ocx 47 | 48 | # Delphi autogenerated files (duplicated info) 49 | *.cfg 50 | *.hpp 51 | *Resource.rc 52 | 53 | # Delphi local files (user-specific info) 54 | *.local 55 | *.identcache 56 | *.projdata 57 | *.tvsconfig 58 | *.dsk 59 | 60 | # Delphi history and backups 61 | __history/ 62 | *.~* 63 | 64 | # Castalia statistics file (since XE7 Castalia is distributed with Delphi) 65 | *.stat 66 | 67 | # DUnit configuration 68 | dunit.ini 69 | Unittests.dproj 70 | 71 | # Free Pascal compiler-generated binaries (safe to delete) 72 | *.ppu 73 | *.compiled 74 | *.obj 75 | lib 76 | 77 | # Free Pascal history and backups 78 | backup 79 | *.bak 80 | *.or 81 | 82 | # Free Pascal local files (user-specific info) 83 | *.lps 84 | 85 | # Free Pascal (other) 86 | *.rsj 87 | *.ico 88 | *.res 89 | *.rst 90 | 91 | # Heaptrace 92 | heaptrace.log 93 | /src/test/Unittests.fpcunit.ini 94 | /src/test/log4delphi.log 95 | 96 | # Other 97 | *.log 98 | 99 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # slf4p 2 | A simple logging facade for Object Pascal, developed with Dephi 2009 and Lazarus 2.0. Tested with DUnit and FPCUnit. 3 | 4 | To register a specific logging framework, just add one of the `djLogOver...` units to the project. 5 | 6 | * `djLogOverLog4D` for logging over [Log4D](http://sourceforge.net/projects/log4d/) 7 | * `djLogOverLazLogger` for logging over [LazLogger](http://wiki.lazarus.freepascal.org/LazLogger) 8 | * `djLogOverSimpleLogger` for logging over SimpleLogger (included) 9 | * `djLogOverNOPLogger` for logging over NOPLogger (included) 10 | 11 | Visit the Wiki for additional information. 12 | 13 | -------------------------------------------------------------------------------- /examples/lazlogger/HelloWorld.lpi: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | <UseAppBundle Value="False"/> 15 | <ResourceType Value="res"/> 16 | </General> 17 | <i18n> 18 | <EnableI18N LFM="False"/> 19 | </i18n> 20 | <VersionInfo> 21 | <StringTable ProductVersion=""/> 22 | </VersionInfo> 23 | <BuildModes Count="1"> 24 | <Item1 Name="Default" Default="True"/> 25 | </BuildModes> 26 | <PublishOptions> 27 | <Version Value="2"/> 28 | </PublishOptions> 29 | <RunParams> 30 | <local> 31 | <FormatVersion Value="1"/> 32 | <CommandLineParams Value="--debug-enabled=+demo"/> 33 | </local> 34 | </RunParams> 35 | <RequiredPackages Count="1"> 36 | <Item1> 37 | <PackageName Value="LazUtils"/> 38 | </Item1> 39 | </RequiredPackages> 40 | <Units Count="3"> 41 | <Unit0> 42 | <Filename Value="HelloWorld.lpr"/> 43 | <IsPartOfProject Value="True"/> 44 | </Unit0> 45 | <Unit1> 46 | <Filename Value="..\..\src\main\djLogOverLazLogger.pas"/> 47 | <IsPartOfProject Value="True"/> 48 | </Unit1> 49 | <Unit2> 50 | <Filename Value="..\..\src\main\LazLoggerLogger.pas"/> 51 | <IsPartOfProject Value="True"/> 52 | </Unit2> 53 | </Units> 54 | </ProjectOptions> 55 | <CompilerOptions> 56 | <Version Value="11"/> 57 | <PathDelim Value="\"/> 58 | <Target> 59 | <Filename Value="HelloWorld"/> 60 | </Target> 61 | <SearchPaths> 62 | <IncludeFiles Value="$(ProjOutDir)"/> 63 | <OtherUnitFiles Value="..\..\src\main"/> 64 | <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> 65 | </SearchPaths> 66 | </CompilerOptions> 67 | <Debugging> 68 | <Exceptions Count="3"> 69 | <Item1> 70 | <Name Value="EAbort"/> 71 | </Item1> 72 | <Item2> 73 | <Name Value="ECodetoolError"/> 74 | </Item2> 75 | <Item3> 76 | <Name Value="EFOpenError"/> 77 | </Item3> 78 | </Exceptions> 79 | </Debugging> 80 | </CONFIG> 81 | -------------------------------------------------------------------------------- /examples/lazlogger/HelloWorld.lpr: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | program HelloWorld; 18 | 19 | uses 20 | djLogOverLazLogger, djLogAPI, djLoggerFactory; 21 | 22 | var 23 | Logger: ILogger; 24 | 25 | begin 26 | Logger := TdjLoggerFactory.GetLogger('demo'); 27 | Logger.Info('Hello World'); 28 | 29 | WriteLn('hit any key'); 30 | ReadLn; 31 | end. 32 | 33 | -------------------------------------------------------------------------------- /examples/log4d/HelloWorld.lpi: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <CONFIG> 3 | <ProjectOptions> 4 | <Version Value="9"/> 5 | <PathDelim Value="\"/> 6 | <General> 7 | <Flags> 8 | <MainUnitHasCreateFormStatements Value="False"/> 9 | <MainUnitHasTitleStatement Value="False"/> 10 | </Flags> 11 | <SessionStorage Value="InProjectDir"/> 12 | <MainUnit Value="0"/> 13 | <Title Value="HelloWorld"/> 14 | <UseAppBundle Value="False"/> 15 | <ResourceType Value="res"/> 16 | </General> 17 | <i18n> 18 | <EnableI18N LFM="False"/> 19 | </i18n> 20 | <VersionInfo> 21 | <StringTable ProductVersion=""/> 22 | </VersionInfo> 23 | <BuildModes Count="1"> 24 | <Item1 Name="Default" Default="True"/> 25 | </BuildModes> 26 | <PublishOptions> 27 | <Version Value="2"/> 28 | </PublishOptions> 29 | <RunParams> 30 | <local> 31 | <FormatVersion Value="1"/> 32 | <CommandLineParams Value="--debug-enabled=+demo"/> 33 | </local> 34 | </RunParams> 35 | <RequiredPackages Count="1"> 36 | <Item1> 37 | <PackageName Value="LazUtils"/> 38 | </Item1> 39 | </RequiredPackages> 40 | <Units Count="3"> 41 | <Unit0> 42 | <Filename Value="HelloWorld.lpr"/> 43 | <IsPartOfProject Value="True"/> 44 | </Unit0> 45 | <Unit1> 46 | <Filename Value="..\..\src\main\djLogOverLazLogger.pas"/> 47 | <IsPartOfProject Value="True"/> 48 | </Unit1> 49 | <Unit2> 50 | <Filename Value="..\..\src\main\LazLoggerLogger.pas"/> 51 | <IsPartOfProject Value="True"/> 52 | </Unit2> 53 | </Units> 54 | </ProjectOptions> 55 | <CompilerOptions> 56 | <Version Value="11"/> 57 | <PathDelim Value="\"/> 58 | <Target> 59 | <Filename Value="HelloWorld"/> 60 | </Target> 61 | <SearchPaths> 62 | <IncludeFiles Value="$(ProjOutDir)"/> 63 | <OtherUnitFiles Value="..\..\src\main;..\..\..\log4d\src"/> 64 | <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> 65 | </SearchPaths> 66 | </CompilerOptions> 67 | <Debugging> 68 | <Exceptions Count="3"> 69 | <Item1> 70 | <Name Value="EAbort"/> 71 | </Item1> 72 | <Item2> 73 | <Name Value="ECodetoolError"/> 74 | </Item2> 75 | <Item3> 76 | <Name Value="EFOpenError"/> 77 | </Item3> 78 | </Exceptions> 79 | </Debugging> 80 | </CONFIG> 81 | -------------------------------------------------------------------------------- /examples/log4d/HelloWorld.lpr: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | program HelloWorld; 18 | 19 | uses 20 | djLogOverLog4D, djLogAPI, djLoggerFactory, LogConsoleAppender, 21 | Log4D; 22 | 23 | var 24 | LogLayout: ILogLayout; 25 | ConsoleAppender: ILogAppender; 26 | FileAppender: ILogAppender; 27 | Logger: ILogger; 28 | 29 | begin 30 | // Log4D configuration: configure console and file logging 31 | LogLayout := TLogPatternLayout.Create(TTCCPattern); 32 | ConsoleAppender := TLogConsoleAppender.Create('console'); 33 | ConsoleAppender.Layout := LogLayout; 34 | TLogBasicConfigurator.Configure(ConsoleAppender); 35 | FileAppender := TLogFileAppender.Create('file', 'log4d.log'); 36 | FileAppender.Layout := LogLayout; 37 | TLogBasicConfigurator.Configure(FileAppender); 38 | 39 | TLogLogger.GetRootLogger.Level := Info; 40 | WriteLn('Logging with Log4D version ' + Log4DVersion); 41 | 42 | 43 | Logger := TdjLoggerFactory.GetLogger('demo'); 44 | Logger.Info('Hello World'); 45 | 46 | 47 | WriteLn('hit any key'); 48 | ReadLn; 49 | end. 50 | 51 | -------------------------------------------------------------------------------- /examples/log4d/LogConsoleAppender.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit LogConsoleAppender; 18 | 19 | interface 20 | 21 | uses 22 | Log4D; 23 | 24 | type 25 | TLogConsoleAppender = class(TLogCustomAppender) 26 | protected 27 | procedure DoAppend(const Message: string); override; 28 | end; 29 | 30 | implementation 31 | 32 | { TLogConsoleAppender } 33 | 34 | procedure TLogConsoleAppender.DoAppend(const Message: string); 35 | begin 36 | if IsConsole then 37 | Write(Message); 38 | end; 39 | 40 | initialization 41 | RegisterAppender(TLogConsoleAppender); 42 | 43 | end. 44 | -------------------------------------------------------------------------------- /src/main/LazLoggerLogger.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit LazLoggerLogger; 18 | 19 | interface 20 | 21 | uses 22 | LazLogger, 23 | djLogAPI, SysUtils; 24 | 25 | type 26 | TLazLogLevel = (Trace, Debug, Info, Warn, Error); 27 | 28 | { TLazLoggerLogger } 29 | 30 | TLazLoggerLogger = class(TInterfacedObject, ILogger) 31 | private 32 | FLevel: TLazLogLevel; 33 | 34 | FName: string; 35 | 36 | LogGroup: PLazLoggerLogGroup; 37 | 38 | function LevelAsString(const ALogLevel: TLazLogLevel): string; 39 | 40 | function IsEnabledFor(ALogLevel: TLazLogLevel): Boolean; 41 | 42 | procedure SetLevel(AValue: TLazLogLevel); 43 | 44 | procedure WriteMsg(const ALogLevel: TLazLogLevel; const AMsg: string); overload; 45 | procedure WriteMsg(const ALogLevel: TLazLogLevel; const AMsg: string; const AException: Exception); overload; 46 | 47 | public 48 | constructor Create(const AName: string); 49 | 50 | procedure Debug(const AMsg: string); overload; 51 | procedure Debug(const AFormat: string; const AArgs: array of const); overload; 52 | procedure Debug(const AMsg: string; const AException: Exception); overload; 53 | 54 | procedure Error(const AMsg: string); overload; 55 | procedure Error(const AFormat: string; const AArgs: array of const); overload; 56 | procedure Error(const AMsg: string; const AException: Exception); overload; 57 | 58 | procedure Info(const AMsg: string); overload; 59 | procedure Info(const AFormat: string; const AArgs: array of const); overload; 60 | procedure Info(const AMsg: string; const AException: Exception); overload; 61 | 62 | procedure Warn(const AMsg: string); overload; 63 | procedure Warn(const AFormat: string; const AArgs: array of const); overload; 64 | procedure Warn(const AMsg: string; const AException: Exception); overload; 65 | 66 | procedure Trace(const AMsg: string); overload; 67 | procedure Trace(const AFormat: string; const AArgs: array of const); overload; 68 | procedure Trace(const AMsg: string; const AException: Exception); overload; 69 | 70 | function Name: string; 71 | 72 | function IsDebugEnabled: Boolean; 73 | function IsErrorEnabled: Boolean; 74 | function IsInfoEnabled: Boolean; 75 | function IsWarnEnabled: Boolean; 76 | function IsTraceEnabled: Boolean; 77 | 78 | property Level: TLazLogLevel read FLevel write SetLevel; 79 | 80 | end; 81 | 82 | TLazLoggerFactory = class(TInterfacedObject, ILoggerFactory) 83 | public 84 | function GetLogger(const AName: string): ILogger; 85 | end; 86 | 87 | var 88 | DefaultLevel: TLazLogLevel; 89 | 90 | implementation 91 | 92 | const 93 | MilliSecsPerDay = 24 * 60 * 60 * 1000; 94 | SBlanks = ' '; 95 | 96 | var 97 | { Start time for the logging process - to compute elapsed time. } 98 | StartTime: TDateTime; 99 | 100 | { The elapsed time since package start up (in milliseconds). } 101 | function GetElapsedTime: LongInt; 102 | begin 103 | Result := Round((Now - StartTime) * MilliSecsPerDay); 104 | end; 105 | 106 | { TLazLoggerLogger } 107 | 108 | constructor TLazLoggerLogger.Create(const AName: string); 109 | begin 110 | FName := AName; 111 | 112 | LogGroup := DebugLogger.RegisterLogGroup(AName, True); // always on 113 | // DebugLogger.ParamForEnabledLogGroups := '--debug-enabled='; 114 | end; 115 | 116 | function TLazLoggerLogger.LevelAsString(const ALogLevel: TLazLogLevel): string; 117 | begin 118 | case ALogLevel of 119 | LazLoggerLogger.Trace: LevelAsString := 'TRACE'; 120 | LazLoggerLogger.Debug: LevelAsString := 'DEBUG'; 121 | LazLoggerLogger.Info: LevelAsString := 'INFO'; 122 | LazLoggerLogger.Warn: LevelAsString := 'WARN'; 123 | LazLoggerLogger.Error: LevelAsString := 'ERROR'; 124 | end; 125 | end; 126 | 127 | function TLazLoggerLogger.IsEnabledFor(ALogLevel: TLazLogLevel): Boolean; 128 | begin 129 | Result := Ord(FLevel) <= Ord(ALogLevel); 130 | end; 131 | 132 | procedure TLazLoggerLogger.WriteMsg(const ALogLevel: TLazLogLevel; const AMsg: string); 133 | begin 134 | LazLogger.DebugLn( 135 | LogGroup, IntToStr(GetElapsedTime) + ' ' + LevelAsString(ALogLevel) + ' ' 136 | + Name + ' - ' + AMsg); 137 | end; 138 | 139 | procedure TLazLoggerLogger.WriteMsg(const ALogLevel: TLazLogLevel; const AMsg: string; 140 | const AException: Exception); 141 | begin 142 | WriteMsg(ALogLevel, 143 | AMsg + SLineBreak 144 | + SBlanks + AException.ClassName + SLineBreak 145 | + SBlanks + AException.Message); 146 | end; 147 | 148 | procedure TLazLoggerLogger.SetLevel(AValue: TLazLogLevel); 149 | begin 150 | if FLevel = AValue then Exit; 151 | 152 | FLevel := AValue; 153 | end; 154 | 155 | procedure TLazLoggerLogger.Debug(const AMsg: string); 156 | begin 157 | if IsDebugEnabled then 158 | WriteMsg(LazLoggerLogger.Debug, AMsg); 159 | end; 160 | 161 | procedure TLazLoggerLogger.Debug(const AFormat: string; const AArgs: array of const); 162 | begin 163 | if IsDebugEnabled then 164 | WriteMsg(LazLoggerLogger.Debug, Format(AFormat, AArgs)); 165 | end; 166 | 167 | procedure TLazLoggerLogger.Debug(const AMsg: string; const AException: Exception); 168 | begin 169 | if IsDebugEnabled then 170 | WriteMsg(LazLoggerLogger.Debug, AMsg, AException); 171 | end; 172 | 173 | procedure TLazLoggerLogger.Error(const AMsg: string; const AException: Exception); 174 | begin 175 | if IsErrorEnabled then 176 | WriteMsg(LazLoggerLogger.Error, AMsg, AException); 177 | end; 178 | 179 | procedure TLazLoggerLogger.Error(const AFormat: string; 180 | const AArgs: array of const); 181 | begin 182 | if IsErrorEnabled then 183 | WriteMsg(LazLoggerLogger.Error, Format(AFormat, AArgs)); 184 | end; 185 | 186 | procedure TLazLoggerLogger.Error(const AMsg: string); 187 | begin 188 | if IsErrorEnabled then 189 | WriteMsg(LazLoggerLogger.Error, AMsg); 190 | end; 191 | 192 | function TLazLoggerLogger.IsDebugEnabled: Boolean; 193 | begin 194 | Result := IsEnabledFor(LazLoggerLogger.Debug); 195 | end; 196 | 197 | function TLazLoggerLogger.IsErrorEnabled: Boolean; 198 | begin 199 | Result := IsEnabledFor(LazLoggerLogger.Error); 200 | end; 201 | 202 | function TLazLoggerLogger.IsInfoEnabled: Boolean; 203 | begin 204 | Result := IsEnabledFor(LazLoggerLogger.Info); 205 | end; 206 | 207 | function TLazLoggerLogger.IsTraceEnabled: Boolean; 208 | begin 209 | Result := IsEnabledFor(LazLoggerLogger.Trace); 210 | end; 211 | 212 | function TLazLoggerLogger.IsWarnEnabled: Boolean; 213 | begin 214 | Result := IsEnabledFor(LazLoggerLogger.Warn); 215 | end; 216 | 217 | procedure TLazLoggerLogger.Info(const AFormat: string; 218 | const AArgs: array of const); 219 | begin 220 | if IsInfoEnabled then 221 | WriteMsg(LazLoggerLogger.Info, Format(AFormat, AArgs)); 222 | end; 223 | 224 | procedure TLazLoggerLogger.Info(const AMsg: string); 225 | begin 226 | if IsInfoEnabled then 227 | WriteMsg(LazLoggerLogger.Info, AMsg); 228 | end; 229 | 230 | procedure TLazLoggerLogger.Info(const AMsg: string; const AException: Exception); 231 | begin 232 | if IsInfoEnabled then 233 | WriteMsg(LazLoggerLogger.Info, AMsg, AException); 234 | end; 235 | 236 | procedure TLazLoggerLogger.Trace(const AMsg: string; const AException: Exception); 237 | begin 238 | if IsTraceEnabled then 239 | WriteMsg(LazLoggerLogger.Trace, AMsg, AException); 240 | end; 241 | 242 | function TLazLoggerLogger.Name: string; 243 | begin 244 | Result := FName; 245 | end; 246 | 247 | procedure TLazLoggerLogger.Trace(const AFormat: string; 248 | const AArgs: array of const); 249 | begin 250 | if IsTraceEnabled then 251 | WriteMsg(LazLoggerLogger.Trace, Format(AFormat, AArgs)); 252 | end; 253 | 254 | procedure TLazLoggerLogger.Trace(const AMsg: string); 255 | begin 256 | if IsTraceEnabled then 257 | WriteMsg(LazLoggerLogger.Trace, AMsg); 258 | end; 259 | 260 | procedure TLazLoggerLogger.Warn(const AMsg: string; const AException: Exception); 261 | begin 262 | if IsWarnEnabled then 263 | WriteMsg(LazLoggerLogger.Warn, AMsg, AException); 264 | end; 265 | 266 | procedure TLazLoggerLogger.Warn(const AFormat: string; 267 | const AArgs: array of const); 268 | begin 269 | if IsWarnEnabled then 270 | WriteMsg(LazLoggerLogger.Warn, Format(AFormat, AArgs)); 271 | end; 272 | 273 | procedure TLazLoggerLogger.Warn(const AMsg: string); 274 | begin 275 | if IsWarnEnabled then 276 | WriteMsg(LazLoggerLogger.Warn, AMsg); 277 | end; 278 | 279 | { TLazLoggerFactory } 280 | 281 | function TLazLoggerFactory.GetLogger(const AName: string): ILogger; 282 | var 283 | Logger: TLazLoggerLogger; 284 | begin 285 | Logger := TLazLoggerLogger.Create(AName); 286 | Logger.Level := DefaultLevel; 287 | Result := Logger; 288 | end; 289 | 290 | initialization 291 | StartTime := Now; 292 | DefaultLevel:= Info; 293 | 294 | end. 295 | -------------------------------------------------------------------------------- /src/main/Log4DLogger.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit Log4DLogger; 18 | 19 | interface 20 | 21 | uses 22 | Log4D, 23 | djLogAPI, SysUtils; 24 | 25 | type 26 | TLog4DLogger = class(TInterfacedObject, ILogger) 27 | private 28 | Delegate: TLogLogger; 29 | 30 | procedure Log(const ALevel: TLogLevel; const AMsg: string); overload; 31 | procedure Log(const ALevel: TLogLevel; const AFormat: string; const AArgs: array of const); overload; 32 | 33 | public 34 | constructor Create(const AName: string); 35 | 36 | procedure Debug(const AMsg: string); overload; 37 | procedure Debug(const AFormat: string; const AArgs: array of const); overload; 38 | procedure Debug(const AMsg: string; const AException: Exception); overload; 39 | 40 | procedure Error(const AMsg: string); overload; 41 | procedure Error(const AFormat: string; const AArgs: array of const); overload; 42 | procedure Error(const AMsg: string; const AException: Exception); overload; 43 | 44 | procedure Info(const AMsg: string); overload; 45 | procedure Info(const AFormat: string; const AArgs: array of const); overload; 46 | procedure Info(const AMsg: string; const AException: Exception); overload; 47 | 48 | procedure Warn(const AMsg: string); overload; 49 | procedure Warn(const AFormat: string; const AArgs: array of const); overload; 50 | procedure Warn(const AMsg: string; const AException: Exception); overload; 51 | 52 | procedure Trace(const AMsg: string); overload; 53 | procedure Trace(const AFormat: string; const AArgs: array of const); overload; 54 | procedure Trace(const AMsg: string; const AException: Exception); overload; 55 | 56 | function Name: string; 57 | 58 | function IsDebugEnabled: Boolean; 59 | function IsErrorEnabled: Boolean; 60 | function IsInfoEnabled: Boolean; 61 | function IsWarnEnabled: Boolean; 62 | function IsTraceEnabled: Boolean; 63 | 64 | end; 65 | 66 | TLog4DLoggerFactory = class(TInterfacedObject, ILoggerFactory) 67 | public 68 | function GetLogger(const AName: string): ILogger; 69 | end; 70 | 71 | implementation 72 | 73 | { TLog4DLogger } 74 | 75 | constructor TLog4DLogger.Create(const AName: string); 76 | begin 77 | inherited Create; 78 | 79 | Delegate := TLogLogger.GetLogger(AName); 80 | end; 81 | 82 | procedure TLog4DLogger.Log(const ALevel: TLogLevel; const AMsg: string); 83 | begin 84 | Delegate.Log(ALevel, AMsg); 85 | end; 86 | 87 | procedure TLog4DLogger.Log(const ALevel: TLogLevel; const AFormat: string; 88 | const AArgs: array of const); 89 | begin 90 | if Delegate.IsEnabledFor(ALevel) then 91 | Delegate.Log(ALevel, Format(AFormat, AArgs)); 92 | end; 93 | 94 | procedure TLog4DLogger.Debug(const AMsg: string); 95 | begin 96 | Log(Log4D.Debug, AMsg); 97 | end; 98 | 99 | procedure TLog4DLogger.Debug(const AFormat: string; const AArgs: array of const); 100 | begin 101 | Log(Log4D.Debug, AFormat, AArgs); 102 | end; 103 | 104 | procedure TLog4DLogger.Debug(const AMsg: string; const AException: Exception); 105 | begin 106 | Delegate.Debug(AMsg, AException); 107 | end; 108 | 109 | procedure TLog4DLogger.Trace(const AMsg: string; const AException: Exception); 110 | begin 111 | Delegate.Trace(AMsg, AException); 112 | end; 113 | 114 | procedure TLog4DLogger.Trace(const AFormat: string; 115 | const AArgs: array of const); 116 | begin 117 | Log(Log4D.Trace, AFormat, AArgs); 118 | end; 119 | 120 | procedure TLog4DLogger.Trace(const AMsg: string); 121 | begin 122 | Log(Log4D.Trace, AMsg); 123 | end; 124 | 125 | procedure TLog4DLogger.Warn(const AMsg: string; const AException: Exception); 126 | begin 127 | Delegate.Warn(AMsg, AException); 128 | end; 129 | 130 | procedure TLog4DLogger.Warn(const AFormat: string; const AArgs: array of const); 131 | begin 132 | Log(Log4D.Warn, AFormat, AArgs); 133 | end; 134 | 135 | procedure TLog4DLogger.Warn(const AMsg: string); 136 | begin 137 | Log(Log4D.Warn, AMsg); 138 | end; 139 | 140 | procedure TLog4DLogger.Error(const AFormat: string; 141 | const AArgs: array of const); 142 | begin 143 | Log(Log4D.Error, AFormat, AArgs); 144 | end; 145 | 146 | procedure TLog4DLogger.Error(const AMsg: string); 147 | begin 148 | Log(Log4D.Error, AMsg); 149 | end; 150 | 151 | procedure TLog4DLogger.Error(const AMsg: string; const AException: Exception); 152 | begin 153 | Delegate.Error(AMsg, AException); 154 | end; 155 | 156 | function TLog4DLogger.Name: string; 157 | begin 158 | Result := Delegate.Name; 159 | end; 160 | 161 | procedure TLog4DLogger.Info(const AMsg: string; const AException: Exception); 162 | begin 163 | Delegate.Info(AMsg, AException); 164 | end; 165 | 166 | function TLog4DLogger.IsDebugEnabled: Boolean; 167 | begin 168 | Result := Delegate.IsDebugEnabled; 169 | end; 170 | 171 | function TLog4DLogger.IsErrorEnabled: Boolean; 172 | begin 173 | Result := Delegate.IsErrorEnabled; 174 | end; 175 | 176 | function TLog4DLogger.IsInfoEnabled: Boolean; 177 | begin 178 | Result := Delegate.IsInfoEnabled; 179 | end; 180 | 181 | function TLog4DLogger.IsTraceEnabled: Boolean; 182 | begin 183 | Result := Delegate.IsTraceEnabled; 184 | end; 185 | 186 | function TLog4DLogger.IsWarnEnabled: Boolean; 187 | begin 188 | Result := Delegate.IsWarnEnabled; 189 | end; 190 | 191 | procedure TLog4DLogger.Info(const AFormat: string; const AArgs: array of const); 192 | begin 193 | Log(Log4D.Info, AFormat, AArgs); 194 | end; 195 | 196 | procedure TLog4DLogger.Info(const AMsg: string); 197 | begin 198 | Log(Log4D.Info, AMsg); 199 | end; 200 | 201 | { TLog4DLoggerFactory } 202 | 203 | function TLog4DLoggerFactory.GetLogger(const AName: string): ILogger; 204 | begin 205 | Result := TLog4DLogger.Create(AName); 206 | end; 207 | 208 | end. 209 | -------------------------------------------------------------------------------- /src/main/NOPLogger.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit NOPLogger; 18 | 19 | interface 20 | 21 | uses 22 | djLogAPI, SysUtils; 23 | 24 | {$HINTS OFF} 25 | 26 | type 27 | TNOPLogger = class(TInterfacedObject, ILogger) 28 | private 29 | FName: string; 30 | 31 | public 32 | constructor Create(const AName: string); 33 | 34 | procedure Debug(const AMsg: string); overload; 35 | procedure Debug(const AFormat: string; const AArgs: array of const); overload; 36 | procedure Debug(const AMsg: string; const AException: Exception); overload; 37 | 38 | procedure Error(const AMsg: string); overload; 39 | procedure Error(const AFormat: string; const AArgs: array of const); overload; 40 | procedure Error(const AMsg: string; const AException: Exception); overload; 41 | 42 | procedure Info(const AMsg: string); overload; 43 | procedure Info(const AFormat: string; const AArgs: array of const); overload; 44 | procedure Info(const AMsg: string; const AException: Exception); overload; 45 | 46 | procedure Warn(const AMsg: string); overload; 47 | procedure Warn(const AFormat: string; const AArgs: array of const); overload; 48 | procedure Warn(const AMsg: string; const AException: Exception); overload; 49 | 50 | procedure Trace(const AMsg: string); overload; 51 | procedure Trace(const AFormat: string; const AArgs: array of const); overload; 52 | procedure Trace(const AMsg: string; const AException: Exception); overload; 53 | 54 | function Name: string; 55 | 56 | function IsDebugEnabled: Boolean; 57 | function IsErrorEnabled: Boolean; 58 | function IsInfoEnabled: Boolean; 59 | function IsWarnEnabled: Boolean; 60 | function IsTraceEnabled: Boolean; 61 | 62 | end; 63 | 64 | TNOPLoggerFactory = class(TInterfacedObject, ILoggerFactory) 65 | public 66 | function GetLogger(const AName: string): ILogger; 67 | end; 68 | 69 | implementation 70 | 71 | { TNOPLogger } 72 | 73 | constructor TNOPLogger.Create(const AName: string); 74 | begin 75 | FName := AName; 76 | end; 77 | 78 | procedure TNOPLogger.Debug(const AMsg: string); 79 | begin 80 | 81 | end; 82 | 83 | procedure TNOPLogger.Debug(const AFormat: string; const AArgs: array of const); 84 | begin 85 | 86 | end; 87 | 88 | procedure TNOPLogger.Debug(const AMsg: string; const AException: Exception); 89 | begin 90 | 91 | end; 92 | 93 | procedure TNOPLogger.Error(const AMsg: string; const AException: Exception); 94 | begin 95 | 96 | end; 97 | 98 | function TNOPLogger.Name: string; 99 | begin 100 | Result := FName; 101 | end; 102 | 103 | procedure TNOPLogger.Error(const AFormat: string; const AArgs: array of const); 104 | begin 105 | 106 | end; 107 | 108 | procedure TNOPLogger.Error(const AMsg: string); 109 | begin 110 | 111 | end; 112 | 113 | procedure TNOPLogger.Info(const AMsg: string; const AException: Exception); 114 | begin 115 | 116 | end; 117 | 118 | function TNOPLogger.IsDebugEnabled: Boolean; 119 | begin 120 | Result := False; 121 | end; 122 | 123 | function TNOPLogger.IsErrorEnabled: Boolean; 124 | begin 125 | Result := False; 126 | end; 127 | 128 | function TNOPLogger.IsInfoEnabled: Boolean; 129 | begin 130 | Result := False; 131 | end; 132 | 133 | function TNOPLogger.IsTraceEnabled: Boolean; 134 | begin 135 | Result := False; 136 | end; 137 | 138 | function TNOPLogger.IsWarnEnabled: Boolean; 139 | begin 140 | Result := False; 141 | end; 142 | 143 | procedure TNOPLogger.Info(const AFormat: string; const AArgs: array of const); 144 | begin 145 | 146 | end; 147 | 148 | procedure TNOPLogger.Info(const AMsg: string); 149 | begin 150 | 151 | end; 152 | 153 | procedure TNOPLogger.Trace(const AMsg: string; const AException: Exception); 154 | begin 155 | 156 | end; 157 | 158 | procedure TNOPLogger.Trace(const AFormat: string; const AArgs: array of const); 159 | begin 160 | 161 | end; 162 | 163 | procedure TNOPLogger.Trace(const AMsg: string); 164 | begin 165 | 166 | end; 167 | 168 | procedure TNOPLogger.Warn(const AMsg: string; const AException: Exception); 169 | begin 170 | 171 | end; 172 | 173 | procedure TNOPLogger.Warn(const AFormat: string; const AArgs: array of const); 174 | begin 175 | 176 | end; 177 | 178 | procedure TNOPLogger.Warn(const AMsg: string); 179 | begin 180 | 181 | end; 182 | 183 | { TNOPLoggerFactory } 184 | 185 | function TNOPLoggerFactory.GetLogger(const AName: string): ILogger; 186 | begin 187 | Result := TNOPLogger.Create(AName); 188 | end; 189 | 190 | end. 191 | -------------------------------------------------------------------------------- /src/main/SimpleLogger.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit SimpleLogger; 18 | 19 | interface 20 | 21 | uses 22 | djLogAPI, Classes, SysUtils; 23 | 24 | type 25 | TSimpleLogLevel = (Trace, Debug, Info, Warn, Error); 26 | 27 | { TSimpleLogger } 28 | 29 | TSimpleLogger = class(TInterfacedObject, ILogger) 30 | private 31 | FDateTimeFormat: string; 32 | FLevel: TSimpleLogLevel; 33 | FName: string; 34 | FShowDateTime: Boolean; 35 | 36 | function GetElapsedTime: LongInt; 37 | function DateTimeStr: string; 38 | function LevelAsString(const ALogLevel: TSimpleLogLevel): string; 39 | 40 | function IsEnabledFor(ALogLevel: TSimpleLogLevel): Boolean; 41 | 42 | procedure WriteMsg(const ALogLevel: TSimpleLogLevel; const AMsg: string); 43 | overload; 44 | procedure WriteMsg(const ALogLevel: TSimpleLogLevel; const AMsg: string; 45 | const AException: Exception); overload; 46 | 47 | procedure SetLevel(AValue: TSimpleLogLevel); 48 | 49 | public 50 | constructor Create(const AName: string); 51 | 52 | procedure Debug(const AMsg: string); overload; 53 | procedure Debug(const AFormat: string; const AArgs: array of const); overload; 54 | procedure Debug(const AMsg: string; const AException: Exception); overload; 55 | 56 | procedure Error(const AMsg: string); overload; 57 | procedure Error(const AFormat: string; const AArgs: array of const); overload; 58 | procedure Error(const AMsg: string; const AException: Exception); overload; 59 | 60 | procedure Info(const AMsg: string); overload; 61 | procedure Info(const AFormat: string; const AArgs: array of const); overload; 62 | procedure Info(const AMsg: string; const AException: Exception); overload; 63 | 64 | procedure Warn(const AMsg: string); overload; 65 | procedure Warn(const AFormat: string; const AArgs: array of const); overload; 66 | procedure Warn(const AMsg: string; const AException: Exception); overload; 67 | 68 | procedure Trace(const AMsg: string); overload; 69 | procedure Trace(const AFormat: string; const AArgs: array of const); overload; 70 | procedure Trace(const AMsg: string; const AException: Exception); overload; 71 | 72 | function Name: string; 73 | 74 | function IsDebugEnabled: Boolean; 75 | function IsErrorEnabled: Boolean; 76 | function IsInfoEnabled: Boolean; 77 | function IsWarnEnabled: Boolean; 78 | function IsTraceEnabled: Boolean; 79 | 80 | property DateTimeFormat: string read FDateTimeFormat write FDateTimeFormat; 81 | property Level: TSimpleLogLevel read FLevel write SetLevel; 82 | property ShowDateTime: Boolean read FShowDateTime write FShowDateTime; 83 | end; 84 | 85 | { TSimpleLoggerFactory } 86 | 87 | TSimpleLoggerFactory = class(TInterfacedObject, ILoggerFactory) 88 | public 89 | constructor Create; 90 | destructor Destroy; override; 91 | 92 | function GetLogger(const AName: string): ILogger; 93 | end; 94 | 95 | 96 | procedure Configure(const AStrings: TStrings); overload; 97 | 98 | procedure Configure(const AKey, AValue: string); overload; 99 | 100 | implementation 101 | 102 | type 103 | { TSimpleLoggerConfiguration } 104 | TSimpleLoggerConfiguration = class(TObject) 105 | private 106 | FDateTimeFormat: string; 107 | FLevel: TSimpleLogLevel; 108 | FShowDateTime: Boolean; 109 | public 110 | constructor Create; 111 | 112 | procedure Configure(const AStrings: TStrings); overload; 113 | 114 | procedure Configure(const AKey, AValue: string); overload; 115 | 116 | property DateTimeFormat: string read FDateTimeFormat; 117 | property Level: TSimpleLogLevel read FLevel; 118 | property ShowDateTime: Boolean read FShowDateTime; 119 | end; 120 | 121 | resourcestring 122 | SNoFactory = 'A logger factory must be created before calling Configure'; 123 | 124 | const 125 | MilliSecsPerDay = 24 * 60 * 60 * 1000; 126 | SBlanks = ' '; 127 | 128 | var 129 | { Contains logger configuration } 130 | Config: TSimpleLoggerConfiguration; 131 | 132 | { Start time for the logging process - to compute elapsed time. } 133 | StartTime: TDateTime; 134 | 135 | procedure Configure(const AStrings: TStrings); 136 | begin 137 | Assert(Assigned(Config), SNoFactory); 138 | 139 | Config.Configure(AStrings); 140 | end; 141 | 142 | procedure Configure(const AKey, AValue: string); 143 | begin 144 | Assert(Assigned(Config), SNoFactory); 145 | 146 | Config.Configure(AKey, AValue); 147 | end; 148 | 149 | { TSimpleLoggerConfiguration } 150 | 151 | constructor TSimpleLoggerConfiguration.Create; 152 | begin 153 | FLevel := Info; 154 | end; 155 | 156 | procedure TSimpleLoggerConfiguration.Configure(const AStrings: TStrings); 157 | var 158 | Line: string; 159 | begin 160 | Line := LowerCase(AStrings.Values['defaultLogLevel']); 161 | if Line <> '' then 162 | begin 163 | if Line = 'trace' then FLevel := Trace 164 | else if Line = 'debug' then FLevel := Debug 165 | else if Line = 'info' then FLevel := Info 166 | else if Line = 'warn' then FLevel := Warn 167 | else if Line = 'error' then FLevel := Error; 168 | end; 169 | 170 | Line := LowerCase(AStrings.Values['showDateTime']); 171 | if Line <> '' then 172 | begin 173 | FShowDateTime := (Line = 'true'); 174 | FDateTimeFormat := 'hh:nn:ss.zzz'; 175 | end; 176 | 177 | Line := LowerCase(AStrings.Values['dateTimeFormat']); 178 | if Line <> '' then 179 | begin 180 | try 181 | FormatDateTime(Line, Now); 182 | FDateTimeFormat := Line; 183 | except 184 | on E: Exception do 185 | begin 186 | FDateTimeFormat := ''; 187 | end; 188 | end; 189 | end; 190 | end; 191 | 192 | procedure TSimpleLoggerConfiguration.Configure(const AKey, AValue: string); 193 | var 194 | SL: TStrings; 195 | begin 196 | SL := TStringList.Create; 197 | try 198 | SL.Values[AKey] := AValue; 199 | Configure(SL); 200 | finally 201 | SL.Free; 202 | end; 203 | end; 204 | 205 | { TSimpleLogger } 206 | 207 | constructor TSimpleLogger.Create(const AName: string); 208 | begin 209 | FName := AName; 210 | end; 211 | 212 | function TSimpleLogger.LevelAsString(const ALogLevel: TSimpleLogLevel): string; 213 | begin 214 | case ALogLevel of 215 | SimpleLogger.Trace: Result := 'TRACE'; 216 | SimpleLogger.Debug: Result := 'DEBUG'; 217 | SimpleLogger.Info: Result := 'INFO'; 218 | SimpleLogger.Warn: Result := 'WARN'; 219 | SimpleLogger.Error: Result := 'ERROR'; 220 | end; 221 | end; 222 | 223 | function TSimpleLogger.IsEnabledFor(ALogLevel: TSimpleLogLevel): Boolean; 224 | begin 225 | Result := Ord(FLevel) <= Ord(ALogLevel); 226 | end; 227 | 228 | procedure TSimpleLogger.WriteMsg(const ALogLevel: TSimpleLogLevel; const AMsg: string); 229 | begin 230 | WriteLn(DateTimeStr + ' ' 231 | + LevelAsString(ALogLevel) + ' ' 232 | + Name + ' - ' 233 | + AMsg); 234 | end; 235 | 236 | function TSimpleLogger.DateTimeStr: string; 237 | begin 238 | if ShowDateTime and (DateTimeFormat <> '') then 239 | Result := FormatDateTime(DateTimeFormat, Now) 240 | else 241 | Result := IntToStr(GetElapsedTime); 242 | end; 243 | 244 | { The elapsed time since package start up (in milliseconds). } 245 | function TSimpleLogger.GetElapsedTime: LongInt; 246 | begin 247 | Result := Round((Now - StartTime) * MilliSecsPerDay); 248 | end; 249 | 250 | procedure TSimpleLogger.WriteMsg(const ALogLevel: TSimpleLogLevel; const AMsg: string; 251 | const AException: Exception); 252 | begin 253 | WriteMsg(ALogLevel, 254 | AMsg + SLineBreak 255 | + SBlanks + AException.ClassName + SLineBreak 256 | + SBlanks + AException.Message); 257 | end; 258 | 259 | procedure TSimpleLogger.SetLevel(AValue: TSimpleLogLevel); 260 | begin 261 | if FLevel = AValue then Exit; 262 | 263 | FLevel := AValue; 264 | end; 265 | 266 | procedure TSimpleLogger.Debug(const AMsg: string); 267 | begin 268 | if IsDebugEnabled then 269 | WriteMsg(SimpleLogger.Debug, AMsg); 270 | end; 271 | 272 | procedure TSimpleLogger.Debug(const AFormat: string; const AArgs: array of const); 273 | begin 274 | if IsDebugEnabled then 275 | WriteMsg(SimpleLogger.Debug, Format(AFormat, AArgs)); 276 | end; 277 | 278 | procedure TSimpleLogger.Debug(const AMsg: string; const AException: Exception); 279 | begin 280 | if IsDebugEnabled then 281 | WriteMsg(SimpleLogger.Debug, AMsg, AException); 282 | end; 283 | 284 | procedure TSimpleLogger.Error(const AMsg: string; const AException: Exception); 285 | begin 286 | if IsErrorEnabled then 287 | WriteMsg(SimpleLogger.Error, AMsg, AException); 288 | end; 289 | 290 | function TSimpleLogger.Name: string; 291 | begin 292 | Result := FName; 293 | end; 294 | 295 | procedure TSimpleLogger.Error(const AFormat: string; 296 | const AArgs: array of const); 297 | begin 298 | if IsErrorEnabled then 299 | WriteMsg(SimpleLogger.Error, Format(AFormat, AArgs)); 300 | end; 301 | 302 | procedure TSimpleLogger.Error(const AMsg: string); 303 | begin 304 | if IsErrorEnabled then 305 | WriteMsg(SimpleLogger.Error, AMsg); 306 | end; 307 | 308 | function TSimpleLogger.IsDebugEnabled: Boolean; 309 | begin 310 | Result := IsEnabledFor(SimpleLogger.Debug); 311 | end; 312 | 313 | function TSimpleLogger.IsErrorEnabled: Boolean; 314 | begin 315 | Result := IsEnabledFor(SimpleLogger.Error); 316 | end; 317 | 318 | function TSimpleLogger.IsInfoEnabled: Boolean; 319 | begin 320 | Result := IsEnabledFor(SimpleLogger.Info); 321 | end; 322 | 323 | function TSimpleLogger.IsTraceEnabled: Boolean; 324 | begin 325 | Result := IsEnabledFor(SimpleLogger.Trace); 326 | end; 327 | 328 | function TSimpleLogger.IsWarnEnabled: Boolean; 329 | begin 330 | Result := IsEnabledFor(SimpleLogger.Warn); 331 | end; 332 | 333 | procedure TSimpleLogger.Info(const AFormat: string; 334 | const AArgs: array of const); 335 | begin 336 | if IsInfoEnabled then 337 | WriteMsg(SimpleLogger.Info, Format(AFormat, AArgs)); 338 | end; 339 | 340 | procedure TSimpleLogger.Info(const AMsg: string); 341 | begin 342 | if IsInfoEnabled then 343 | WriteMsg(SimpleLogger.Info, AMsg); 344 | end; 345 | 346 | procedure TSimpleLogger.Info(const AMsg: string; const AException: Exception); 347 | begin 348 | if IsInfoEnabled then 349 | WriteMsg(SimpleLogger.Info, AMsg, AException); 350 | end; 351 | 352 | procedure TSimpleLogger.Trace(const AMsg: string; const AException: Exception); 353 | begin 354 | if IsTraceEnabled then 355 | WriteMsg(SimpleLogger.Trace, AMsg, AException); 356 | end; 357 | 358 | procedure TSimpleLogger.Trace(const AFormat: string; 359 | const AArgs: array of const); 360 | begin 361 | if IsTraceEnabled then 362 | WriteMsg(SimpleLogger.Trace, Format(AFormat, AArgs)); 363 | end; 364 | 365 | procedure TSimpleLogger.Trace(const AMsg: string); 366 | begin 367 | if IsTraceEnabled then 368 | WriteMsg(SimpleLogger.Trace, AMsg); 369 | end; 370 | 371 | procedure TSimpleLogger.Warn(const AMsg: string; const AException: Exception); 372 | begin 373 | if IsWarnEnabled then 374 | WriteMsg(SimpleLogger.Warn, AMsg, AException); 375 | end; 376 | 377 | procedure TSimpleLogger.Warn(const AFormat: string; 378 | const AArgs: array of const); 379 | begin 380 | if IsWarnEnabled then 381 | WriteMsg(SimpleLogger.Warn, Format(AFormat, AArgs)); 382 | end; 383 | 384 | procedure TSimpleLogger.Warn(const AMsg: string); 385 | begin 386 | if IsWarnEnabled then 387 | WriteMsg(SimpleLogger.Warn, AMsg); 388 | end; 389 | 390 | { TSimpleLoggerFactory } 391 | 392 | constructor TSimpleLoggerFactory.Create; 393 | begin 394 | inherited Create; 395 | 396 | end; 397 | 398 | destructor TSimpleLoggerFactory.Destroy; 399 | begin 400 | 401 | inherited; 402 | end; 403 | 404 | function TSimpleLoggerFactory.GetLogger(const AName: string): ILogger; 405 | var 406 | Logger: TSimpleLogger; 407 | begin 408 | Logger := TSimpleLogger.Create(AName); 409 | 410 | Logger.DateTimeFormat := Config.DateTimeFormat; 411 | Logger.Level := Config.Level; 412 | Logger.ShowDateTime := Config.ShowDateTime; 413 | 414 | Result := Logger; 415 | end; 416 | 417 | initialization 418 | StartTime := Now; 419 | Config := TSimpleLoggerConfiguration.Create; 420 | 421 | finalization 422 | Config.Free; 423 | 424 | end. 425 | -------------------------------------------------------------------------------- /src/main/StringsLogger.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit StringsLogger; 18 | 19 | interface 20 | 21 | uses 22 | djLogAPI, Classes, SysUtils; 23 | 24 | type 25 | TLogLevel = (Trace, Debug, Info, Warn, Error); 26 | 27 | { TStringsLogger } 28 | 29 | TStringsLogger = class(TInterfacedObject, ILogger) 30 | private 31 | FStrings: TStringBuilder; 32 | FDateTimeFormat: string; 33 | FLevel: TLogLevel; 34 | FName: string; 35 | FShowDateTime: Boolean; 36 | 37 | function GetElapsedTime: LongInt; 38 | function DateTimeStr: string; 39 | function LevelAsString(const ALogLevel: TLogLevel): string; 40 | 41 | function IsEnabledFor(ALogLevel: TLogLevel): Boolean; 42 | 43 | procedure WriteMsg(const ALogLevel: TLogLevel; const AMsg: string); 44 | overload; 45 | procedure WriteMsg(const ALogLevel: TLogLevel; const AMsg: string; 46 | const AException: Exception); overload; 47 | 48 | procedure SetLevel(AValue: TLogLevel); 49 | 50 | public 51 | constructor Create(const AName: string; const AStrings: TStringBuilder); 52 | 53 | procedure Debug(const AMsg: string); overload; 54 | procedure Debug(const AFormat: string; const AArgs: array of const); overload; 55 | procedure Debug(const AMsg: string; const AException: Exception); overload; 56 | 57 | procedure Error(const AMsg: string); overload; 58 | procedure Error(const AFormat: string; const AArgs: array of const); overload; 59 | procedure Error(const AMsg: string; const AException: Exception); overload; 60 | 61 | procedure Info(const AMsg: string); overload; 62 | procedure Info(const AFormat: string; const AArgs: array of const); overload; 63 | procedure Info(const AMsg: string; const AException: Exception); overload; 64 | 65 | procedure Warn(const AMsg: string); overload; 66 | procedure Warn(const AFormat: string; const AArgs: array of const); overload; 67 | procedure Warn(const AMsg: string; const AException: Exception); overload; 68 | 69 | procedure Trace(const AMsg: string); overload; 70 | procedure Trace(const AFormat: string; const AArgs: array of const); overload; 71 | procedure Trace(const AMsg: string; const AException: Exception); overload; 72 | 73 | function Name: string; 74 | 75 | function IsDebugEnabled: Boolean; 76 | function IsErrorEnabled: Boolean; 77 | function IsInfoEnabled: Boolean; 78 | function IsWarnEnabled: Boolean; 79 | function IsTraceEnabled: Boolean; 80 | 81 | property DateTimeFormat: string read FDateTimeFormat write FDateTimeFormat; 82 | property Level: TLogLevel read FLevel write SetLevel; 83 | property ShowDateTime: Boolean read FShowDateTime write FShowDateTime; 84 | end; 85 | 86 | { TStringsLoggerFactory } 87 | 88 | TStringsLoggerFactory = class(TInterfacedObject, ILoggerFactory) 89 | private 90 | FStrings: TStringBuilder; 91 | public 92 | constructor Create(const AStrings: TStringBuilder); 93 | destructor Destroy; override; 94 | 95 | function GetLogger(const AName: string): ILogger; 96 | end; 97 | 98 | 99 | procedure Configure(const AStrings: TStrings); overload; 100 | 101 | procedure Configure(const AKey, AValue: string); overload; 102 | 103 | implementation 104 | 105 | type 106 | { TStringsLoggerConfiguration } 107 | TStringsLoggerConfiguration = class(TObject) 108 | private 109 | FDateTimeFormat: string; 110 | FLevel: TLogLevel; 111 | FShowDateTime: Boolean; 112 | public 113 | constructor Create; 114 | 115 | procedure Configure(const AStrings: TStrings); overload; 116 | 117 | procedure Configure(const AKey, AValue: string); overload; 118 | 119 | property DateTimeFormat: string read FDateTimeFormat; 120 | property Level: TLogLevel read FLevel; 121 | property ShowDateTime: Boolean read FShowDateTime; 122 | end; 123 | 124 | resourcestring 125 | SNoFactory = 'A logger factory must be created before calling Configure'; 126 | 127 | const 128 | MilliSecsPerDay = 24 * 60 * 60 * 1000; 129 | SBlanks = ' '; 130 | 131 | var 132 | { Contains logger configuration } 133 | Config: TStringsLoggerConfiguration; 134 | 135 | { Start time for the logging process - to compute elapsed time. } 136 | StartTime: TDateTime; 137 | 138 | procedure Configure(const AStrings: TStrings); 139 | begin 140 | Assert(Assigned(Config), SNoFactory); 141 | 142 | Config.Configure(AStrings); 143 | end; 144 | 145 | procedure Configure(const AKey, AValue: string); 146 | begin 147 | Assert(Assigned(Config), SNoFactory); 148 | 149 | Config.Configure(AKey, AValue); 150 | end; 151 | 152 | { TSimpleLoggerConfiguration } 153 | 154 | constructor TStringsLoggerConfiguration.Create; 155 | begin 156 | FLevel := Info; 157 | end; 158 | 159 | procedure TStringsLoggerConfiguration.Configure(const AStrings: TStrings); 160 | var 161 | Line: string; 162 | begin 163 | Line := LowerCase(AStrings.Values['defaultLogLevel']); 164 | if Line <> '' then 165 | begin 166 | if Line = 'trace' then FLevel := Trace 167 | else if Line = 'debug' then FLevel := Debug 168 | else if Line = 'info' then FLevel := Info 169 | else if Line = 'warn' then FLevel := Warn 170 | else if Line = 'error' then FLevel := Error; 171 | end; 172 | 173 | Line := LowerCase(AStrings.Values['showDateTime']); 174 | if Line <> '' then 175 | begin 176 | FShowDateTime := (Line = 'true'); 177 | FDateTimeFormat := 'hh:nn:ss.zzz'; 178 | end; 179 | 180 | Line := LowerCase(AStrings.Values['dateTimeFormat']); 181 | if Line <> '' then 182 | begin 183 | try 184 | FormatDateTime(Line, Now); 185 | FDateTimeFormat := Line; 186 | except 187 | on E: Exception do 188 | begin 189 | FDateTimeFormat := ''; 190 | end; 191 | end; 192 | end; 193 | end; 194 | 195 | procedure TStringsLoggerConfiguration.Configure(const AKey, AValue: string); 196 | var 197 | SL: TStrings; 198 | begin 199 | SL := TStringList.Create; 200 | try 201 | SL.Values[AKey] := AValue; 202 | Configure(SL); 203 | finally 204 | SL.Free; 205 | end; 206 | end; 207 | 208 | { TStringsLogger } 209 | 210 | constructor TStringsLogger.Create(const AName: string; 211 | const AStrings: TStringBuilder); 212 | begin 213 | FName := AName; 214 | FStrings := AStrings; 215 | end; 216 | 217 | function TStringsLogger.LevelAsString(const ALogLevel: TLogLevel): string; 218 | begin 219 | case ALogLevel of 220 | StringsLogger.Trace: Result := 'TRACE'; 221 | StringsLogger.Debug: Result := 'DEBUG'; 222 | StringsLogger.Info: Result := 'INFO'; 223 | StringsLogger.Warn: Result := 'WARN'; 224 | StringsLogger.Error: Result := 'ERROR'; 225 | end; 226 | end; 227 | 228 | function TStringsLogger.IsEnabledFor(ALogLevel: TLogLevel): Boolean; 229 | begin 230 | Result := Ord(FLevel) <= Ord(ALogLevel); 231 | end; 232 | 233 | procedure TStringsLogger.WriteMsg(const ALogLevel: TLogLevel; const AMsg: string); 234 | begin 235 | if Config.ShowDateTime then 236 | begin 237 | FStrings.Append(DateTimeStr + ' '); 238 | end; 239 | 240 | FStrings.Append(LevelAsString(ALogLevel) + ' ' + Name + ' - ' + AMsg + sLinebreak); 241 | end; 242 | 243 | function TStringsLogger.DateTimeStr: string; 244 | begin 245 | if ShowDateTime and (DateTimeFormat <> '') then 246 | Result := FormatDateTime(DateTimeFormat, Now) 247 | else 248 | Result := IntToStr(GetElapsedTime); 249 | end; 250 | 251 | { The elapsed time since package start up (in milliseconds). } 252 | function TStringsLogger.GetElapsedTime: LongInt; 253 | begin 254 | Result := Round((Now - StartTime) * MilliSecsPerDay); 255 | end; 256 | 257 | procedure TStringsLogger.WriteMsg(const ALogLevel: TLogLevel; const AMsg: string; 258 | const AException: Exception); 259 | begin 260 | WriteMsg(ALogLevel, 261 | AMsg + SLineBreak 262 | + SBlanks + AException.ClassName + SLineBreak 263 | + SBlanks + AException.Message); 264 | end; 265 | 266 | procedure TStringsLogger.SetLevel(AValue: TLogLevel); 267 | begin 268 | if FLevel = AValue then Exit; 269 | 270 | FLevel := AValue; 271 | end; 272 | 273 | procedure TStringsLogger.Debug(const AMsg: string); 274 | begin 275 | if IsDebugEnabled then 276 | WriteMsg(StringsLogger.Debug, AMsg); 277 | end; 278 | 279 | procedure TStringsLogger.Debug(const AFormat: string; const AArgs: array of const); 280 | begin 281 | if IsDebugEnabled then 282 | WriteMsg(StringsLogger.Debug, Format(AFormat, AArgs)); 283 | end; 284 | 285 | procedure TStringsLogger.Debug(const AMsg: string; const AException: Exception); 286 | begin 287 | if IsDebugEnabled then 288 | WriteMsg(StringsLogger.Debug, AMsg, AException); 289 | end; 290 | 291 | procedure TStringsLogger.Error(const AMsg: string; const AException: Exception); 292 | begin 293 | if IsErrorEnabled then 294 | WriteMsg(StringsLogger.Error, AMsg, AException); 295 | end; 296 | 297 | function TStringsLogger.Name: string; 298 | begin 299 | Result := FName; 300 | end; 301 | 302 | procedure TStringsLogger.Error(const AFormat: string; 303 | const AArgs: array of const); 304 | begin 305 | if IsErrorEnabled then 306 | WriteMsg(StringsLogger.Error, Format(AFormat, AArgs)); 307 | end; 308 | 309 | procedure TStringsLogger.Error(const AMsg: string); 310 | begin 311 | if IsErrorEnabled then 312 | WriteMsg(StringsLogger.Error, AMsg); 313 | end; 314 | 315 | function TStringsLogger.IsDebugEnabled: Boolean; 316 | begin 317 | Result := IsEnabledFor(StringsLogger.Debug); 318 | end; 319 | 320 | function TStringsLogger.IsErrorEnabled: Boolean; 321 | begin 322 | Result := IsEnabledFor(StringsLogger.Error); 323 | end; 324 | 325 | function TStringsLogger.IsInfoEnabled: Boolean; 326 | begin 327 | Result := IsEnabledFor(StringsLogger.Info); 328 | end; 329 | 330 | function TStringsLogger.IsTraceEnabled: Boolean; 331 | begin 332 | Result := IsEnabledFor(StringsLogger.Trace); 333 | end; 334 | 335 | function TStringsLogger.IsWarnEnabled: Boolean; 336 | begin 337 | Result := IsEnabledFor(StringsLogger.Warn); 338 | end; 339 | 340 | procedure TStringsLogger.Info(const AFormat: string; 341 | const AArgs: array of const); 342 | begin 343 | if IsInfoEnabled then 344 | WriteMsg(StringsLogger.Info, Format(AFormat, AArgs)); 345 | end; 346 | 347 | procedure TStringsLogger.Info(const AMsg: string); 348 | begin 349 | if IsInfoEnabled then 350 | WriteMsg(StringsLogger.Info, AMsg); 351 | end; 352 | 353 | procedure TStringsLogger.Info(const AMsg: string; const AException: Exception); 354 | begin 355 | if IsInfoEnabled then 356 | WriteMsg(StringsLogger.Info, AMsg, AException); 357 | end; 358 | 359 | procedure TStringsLogger.Trace(const AMsg: string; const AException: Exception); 360 | begin 361 | if IsTraceEnabled then 362 | WriteMsg(StringsLogger.Trace, AMsg, AException); 363 | end; 364 | 365 | procedure TStringsLogger.Trace(const AFormat: string; 366 | const AArgs: array of const); 367 | begin 368 | if IsTraceEnabled then 369 | WriteMsg(StringsLogger.Trace, Format(AFormat, AArgs)); 370 | end; 371 | 372 | procedure TStringsLogger.Trace(const AMsg: string); 373 | begin 374 | if IsTraceEnabled then 375 | WriteMsg(StringsLogger.Trace, AMsg); 376 | end; 377 | 378 | procedure TStringsLogger.Warn(const AMsg: string; const AException: Exception); 379 | begin 380 | if IsWarnEnabled then 381 | WriteMsg(StringsLogger.Warn, AMsg, AException); 382 | end; 383 | 384 | procedure TStringsLogger.Warn(const AFormat: string; 385 | const AArgs: array of const); 386 | begin 387 | if IsWarnEnabled then 388 | WriteMsg(StringsLogger.Warn, Format(AFormat, AArgs)); 389 | end; 390 | 391 | procedure TStringsLogger.Warn(const AMsg: string); 392 | begin 393 | if IsWarnEnabled then 394 | WriteMsg(StringsLogger.Warn, AMsg); 395 | end; 396 | 397 | { TStringsLoggerFactory } 398 | 399 | constructor TStringsLoggerFactory.Create(const AStrings: TStringBuilder); 400 | begin 401 | inherited Create; 402 | 403 | FStrings := AStrings; 404 | end; 405 | 406 | destructor TStringsLoggerFactory.Destroy; 407 | begin 408 | 409 | inherited; 410 | end; 411 | 412 | function TStringsLoggerFactory.GetLogger(const AName: string): ILogger; 413 | var 414 | Logger: TStringsLogger; 415 | begin 416 | Logger := TStringsLogger.Create(AName, FStrings); 417 | 418 | Logger.DateTimeFormat := Config.DateTimeFormat; 419 | Logger.Level := Config.Level; 420 | Logger.ShowDateTime := Config.ShowDateTime; 421 | 422 | Result := Logger; 423 | end; 424 | 425 | initialization 426 | StartTime := Now; 427 | Config := TStringsLoggerConfiguration.Create; 428 | 429 | finalization 430 | Config.Free; 431 | 432 | end. 433 | 434 | -------------------------------------------------------------------------------- /src/main/djLogAPI.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit djLogAPI; 18 | 19 | interface 20 | 21 | uses 22 | SysUtils; 23 | 24 | type 25 | ILogger = interface ['{58764670-2414-477F-8CE6-02A418D4CF09}'] 26 | procedure Debug(const AMsg: string); overload; 27 | procedure Debug(const AFormat: string; const AArgs: array of const); overload; 28 | procedure Debug(const AMsg: string; const AException: Exception); overload; 29 | 30 | procedure Error(const AMsg: string); overload; 31 | procedure Error(const AFormat: string; const AArgs: array of const); overload; 32 | procedure Error(const AMsg: string; const AException: Exception); overload; 33 | 34 | procedure Info(const AMsg: string); overload; 35 | procedure Info(const AFormat: string; const AArgs: array of const); overload; 36 | procedure Info(const AMsg: string; const AException: Exception); overload; 37 | 38 | procedure Warn(const AMsg: string); overload; 39 | procedure Warn(const AFormat: string; const AArgs: array of const); overload; 40 | procedure Warn(const AMsg: string; const AException: Exception); overload; 41 | 42 | procedure Trace(const AMsg: string); overload; 43 | procedure Trace(const AFormat: string; const AArgs: array of const); overload; 44 | procedure Trace(const AMsg: string; const AException: Exception); overload; 45 | 46 | function IsDebugEnabled: Boolean; 47 | function IsErrorEnabled: Boolean; 48 | function IsInfoEnabled: Boolean; 49 | function IsWarnEnabled: Boolean; 50 | function IsTraceEnabled: Boolean; 51 | 52 | function Name: string; 53 | 54 | end; 55 | 56 | ILoggerFactory = interface ['{B5EC64AC-85D6-40F1-88CC-EC045D9ED653}'] 57 | function GetLogger(const AName: string): ILogger; 58 | end; 59 | 60 | implementation 61 | 62 | end. 63 | -------------------------------------------------------------------------------- /src/main/djLogOverLazLogger.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit djLogOverLazLogger; 18 | 19 | interface 20 | 21 | implementation 22 | 23 | uses 24 | djLoggerFactory, LazLoggerLogger; 25 | 26 | initialization 27 | RegisterFactory(TLazLoggerFactory.Create); 28 | 29 | end. 30 | 31 | -------------------------------------------------------------------------------- /src/main/djLogOverLog4D.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit djLogOverLog4D; 18 | 19 | interface 20 | 21 | implementation 22 | 23 | uses 24 | djLoggerFactory, Log4DLogger; 25 | 26 | initialization 27 | RegisterFactory(TLog4DLoggerFactory.Create); 28 | 29 | end. 30 | -------------------------------------------------------------------------------- /src/main/djLogOverNOPLogger.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit djLogOverNOPLogger; 18 | 19 | interface 20 | 21 | implementation 22 | 23 | uses 24 | djLoggerFactory, NOPLogger; 25 | 26 | initialization 27 | RegisterFactory(TNOPLoggerFactory.Create); 28 | 29 | end. 30 | -------------------------------------------------------------------------------- /src/main/djLogOverSimpleLogger.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit djLogOverSimpleLogger; 18 | 19 | interface 20 | 21 | implementation 22 | 23 | uses 24 | djLoggerFactory, SimpleLogger; 25 | 26 | initialization 27 | RegisterFactory(TSimpleLoggerFactory.Create); 28 | 29 | end. 30 | -------------------------------------------------------------------------------- /src/main/djLoggerFactory.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit djLoggerFactory; 18 | 19 | interface 20 | 21 | uses 22 | djLogAPI; 23 | 24 | type 25 | TdjLoggerFactory = class(TObject) 26 | public 27 | class function GetILoggerFactory: ILoggerFactory; 28 | class function GetLogger(const AName: string): ILogger; 29 | end; 30 | 31 | procedure RegisterFactory(const AFactory: ILoggerFactory); 32 | 33 | implementation 34 | 35 | uses 36 | NOPLogger, 37 | SysUtils, SyncObjs; 38 | 39 | resourcestring 40 | SLF4PTag = 'SLF4P: '; 41 | NoLoggerFactoryAvailable = 'Logger factory is not assigned'; 42 | UseNOPLogger = 'Defaulting to no-operation (NOP) logger implementation'; 43 | WarnOverwrite = 'Warning: overwriting logger factory!'; 44 | 45 | var 46 | LoggerFactory: ILoggerFactory; 47 | CriticalSection: TCriticalSection; 48 | 49 | procedure RegisterFactory(const AFactory: ILoggerFactory); 50 | begin 51 | Assert(Assigned(AFactory)); 52 | 53 | CriticalSection.Enter; 54 | try 55 | if Assigned(LoggerFactory) and IsConsole then 56 | begin 57 | WriteLn(SLF4PTag + WarnOverwrite); 58 | end; 59 | 60 | LoggerFactory := AFactory; 61 | 62 | finally 63 | CriticalSection.Leave; 64 | end; 65 | end; 66 | 67 | { TdjLoggerFactory } 68 | 69 | class function TdjLoggerFactory.GetILoggerFactory: ILoggerFactory; 70 | begin 71 | Result := LoggerFactory; 72 | end; 73 | 74 | class function TdjLoggerFactory.GetLogger(const AName: string): ILogger; 75 | begin 76 | if not Assigned(LoggerFactory) then 77 | begin 78 | if IsConsole then 79 | begin 80 | WriteLn(SLF4PTag + NoLoggerFactoryAvailable); 81 | WriteLn(SLF4PTag + UseNOPLogger); 82 | end; 83 | RegisterFactory(TNOPLoggerFactory.Create); 84 | end; 85 | 86 | Result := LoggerFactory.GetLogger(AName); 87 | end; 88 | 89 | initialization 90 | CriticalSection := TCriticalSection.Create; 91 | 92 | finalization 93 | CriticalSection.Free; 94 | 95 | end. 96 | -------------------------------------------------------------------------------- /src/test/LazLoggerTests.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit LazLoggerTests; 18 | 19 | interface 20 | 21 | uses 22 | {$IFDEF FPC} 23 | fpcunit,testregistry 24 | {$ELSE} 25 | TestFramework 26 | {$ENDIF}; 27 | 28 | type 29 | 30 | { TLazLoggerTests } 31 | 32 | TLazLoggerTests = class(TTestCase) 33 | published 34 | procedure CreateLogger; 35 | procedure TestDebug; 36 | procedure TestInfo; 37 | 38 | procedure TestWriteFile; 39 | end; 40 | 41 | implementation 42 | 43 | uses 44 | djLogAPI, LazLoggerLogger, 45 | LazLogger, 46 | SysUtils; 47 | 48 | { TLazLoggerTests } 49 | 50 | procedure TLazLoggerTests.CreateLogger; 51 | var 52 | LoggerFactory: ILoggerFactory; 53 | Logger: ILogger; 54 | begin 55 | LoggerFactory := TLazLoggerFactory.Create; 56 | Logger := LoggerFactory.GetLogger('lazlogger1'); 57 | 58 | CheckEquals('lazlogger1', Logger.Name); 59 | end; 60 | 61 | procedure TLazLoggerTests.TestDebug; 62 | var 63 | LoggerFactory: ILoggerFactory; 64 | Logger: ILogger; 65 | E: EAbort; 66 | begin 67 | LoggerFactory := TLazLoggerFactory.Create; 68 | 69 | Logger := LoggerFactory.GetLogger('lazlogger2'); 70 | 71 | Logger.Debug('log4d msg'); 72 | Logger.Debug('log4d msg', ['a', 2, Date]); 73 | 74 | E := EAbort.Create('example'); 75 | Logger.Debug('log4d msg', E); 76 | E.Free; 77 | end; 78 | 79 | procedure TLazLoggerTests.TestInfo; 80 | var 81 | LoggerFactory: ILoggerFactory; 82 | Logger: ILogger; 83 | E: EAbort; 84 | begin 85 | LoggerFactory := TLazLoggerFactory.Create; 86 | 87 | Logger := LoggerFactory.GetLogger('lazlogger3'); 88 | 89 | Logger.Info('simple msg'); 90 | Logger.Info('simple msg', ['a', 2, Date]); 91 | 92 | E := EAbort.Create('simple example exception'); 93 | Logger.Info('simple msg', E); 94 | E.Free; 95 | end; 96 | 97 | procedure TLazLoggerTests.TestWriteFile; 98 | var 99 | LoggerFactory: ILoggerFactory; 100 | Logger: ILogger; 101 | begin 102 | LoggerFactory := TLazLoggerFactory.Create; 103 | 104 | Logger := LoggerFactory.GetLogger('lazlogger4'); 105 | 106 | DebugLogger.LogName := 'lazlogger.log'; 107 | 108 | Logger.Info('logged to lazlogger.log'); 109 | end; 110 | 111 | end. 112 | 113 | -------------------------------------------------------------------------------- /src/test/Log4DLoggerTests.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit Log4DLoggerTests; 18 | 19 | interface 20 | 21 | uses 22 | {$IFDEF FPC} 23 | fpcunit,testregistry 24 | {$ELSE} 25 | TestFramework 26 | {$ENDIF}; 27 | 28 | type 29 | TLog4DLoggerTests = class(TTestCase) 30 | published 31 | procedure CreateLogger; 32 | procedure TestDebug; 33 | procedure TestInfo; 34 | end; 35 | 36 | implementation 37 | 38 | uses 39 | djLogAPI, Log4DLogger, SysUtils; 40 | 41 | { TLog4DLoggerTests } 42 | 43 | procedure TLog4DLoggerTests.CreateLogger; 44 | var 45 | LoggerFactory: ILoggerFactory; 46 | Logger: ILogger; 47 | begin 48 | LoggerFactory := TLog4DLoggerFactory.Create; 49 | Logger := LoggerFactory.GetLogger('log4dlogger'); 50 | 51 | CheckEquals('log4dlogger', Logger.Name); 52 | end; 53 | 54 | procedure TLog4DLoggerTests.TestDebug; 55 | var 56 | LoggerFactory: ILoggerFactory; 57 | Logger: ILogger; 58 | E: EAbort; 59 | begin 60 | LoggerFactory := TLog4DLoggerFactory.Create; 61 | 62 | Logger := LoggerFactory.GetLogger('log4d'); 63 | 64 | Logger.Debug('log4d msg'); 65 | Logger.Debug('log4d msg', ['a', 2, Date]); 66 | 67 | E := EAbort.Create('example'); 68 | Logger.Debug('log4d msg', E); 69 | E.Free; 70 | end; 71 | 72 | procedure TLog4DLoggerTests.TestInfo; 73 | var 74 | LoggerFactory: ILoggerFactory; 75 | Logger: ILogger; 76 | E: EAbort; 77 | begin 78 | LoggerFactory := TLog4DLoggerFactory.Create; 79 | 80 | Logger := LoggerFactory.GetLogger('simple'); 81 | 82 | Logger.Info('simple msg'); 83 | Logger.Info('simple msg', ['a', 2, Date]); 84 | 85 | E := EAbort.Create('simple example exception'); 86 | Logger.Info('simple msg', E); 87 | E.Free; 88 | end; 89 | 90 | end. 91 | -------------------------------------------------------------------------------- /src/test/LoggerFactoryTests.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit LoggerFactoryTests; 18 | 19 | interface 20 | 21 | uses 22 | {$IFDEF FPC} 23 | fpcunit,testregistry 24 | {$ELSE} 25 | TestFramework 26 | {$ENDIF}; 27 | 28 | type 29 | 30 | { TdjLoggerFactoryTests } 31 | 32 | TdjLoggerFactoryTests = class(TTestCase) 33 | published 34 | procedure TestNoFactoryAssigned; 35 | 36 | end; 37 | 38 | implementation 39 | 40 | uses 41 | djLogAPI, djLoggerFactory, SysUtils; 42 | 43 | { TLoggerFactoryTests } 44 | 45 | procedure TdjLoggerFactoryTests.TestNoFactoryAssigned; 46 | var 47 | Logger: ILogger; 48 | begin 49 | // ExpectException(Exception); 50 | Logger := TdjLoggerFactory.GetLogger('djLoggerFactory'); 51 | Logger.Info('logging with NOP logger'); 52 | end; 53 | 54 | 55 | end. 56 | -------------------------------------------------------------------------------- /src/test/NOPLoggerTests.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit NOPLoggerTests; 18 | 19 | interface 20 | 21 | uses 22 | {$IFDEF FPC} 23 | fpcunit,testregistry 24 | {$ELSE} 25 | TestFramework 26 | {$ENDIF}; 27 | 28 | type 29 | TNOPLoggerTests = class(TTestCase) 30 | published 31 | procedure CreateLogger; 32 | procedure TestDebug; 33 | end; 34 | 35 | implementation 36 | 37 | uses 38 | djLogAPI, NOPLogger, SysUtils; 39 | 40 | { TNOPLoggerTests } 41 | 42 | procedure TNOPLoggerTests.CreateLogger; 43 | var 44 | LoggerFactory: ILoggerFactory; 45 | Logger: ILogger; 46 | begin 47 | LoggerFactory := TNOPLoggerFactory.Create; 48 | Logger := LoggerFactory.GetLogger('nop'); 49 | 50 | CheckEquals('nop', Logger.Name); 51 | end; 52 | 53 | procedure TNOPLoggerTests.TestDebug; 54 | var 55 | LoggerFactory: ILoggerFactory; 56 | Logger: ILogger; 57 | E: EAbort; 58 | begin 59 | LoggerFactory := TNOPLoggerFactory.Create; 60 | 61 | Logger := LoggerFactory.GetLogger('nop'); 62 | 63 | Logger.Debug('nop msg'); 64 | Logger.Debug('nop msg', ['a', 2, Date]); 65 | 66 | E := EAbort.Create('example'); 67 | Logger.Debug('nop msg', E); 68 | E.Free; 69 | end; 70 | 71 | end. 72 | -------------------------------------------------------------------------------- /src/test/SimpleLoggerTests.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit SimpleLoggerTests; 18 | 19 | interface 20 | 21 | uses 22 | {$IFDEF FPC} 23 | fpcunit,testregistry 24 | {$ELSE} 25 | TestFramework 26 | {$ENDIF}; 27 | 28 | type 29 | 30 | { TSimpleLoggerTests } 31 | 32 | TSimpleLoggerTests = class(TTestCase) 33 | published 34 | procedure CreateLogger; 35 | procedure TestConfig; 36 | procedure TestConfigShowDateTime; 37 | 38 | procedure TestDebug; 39 | procedure TestInfo; 40 | end; 41 | 42 | implementation 43 | 44 | uses 45 | djLogAPI, SimpleLogger, Classes, SysUtils; 46 | 47 | { TSimpleLoggerTests } 48 | 49 | procedure TSimpleLoggerTests.CreateLogger; 50 | var 51 | LoggerFactory: ILoggerFactory; 52 | Logger: ILogger; 53 | begin 54 | LoggerFactory := TSimpleLoggerFactory.Create; 55 | Logger := LoggerFactory.GetLogger('simple'); 56 | 57 | CheckEquals('simple', Logger.Name); 58 | end; 59 | 60 | procedure TSimpleLoggerTests.TestConfig; 61 | var 62 | S: TStrings; 63 | LoggerFactory: ILoggerFactory; 64 | Logger: ILogger; 65 | begin 66 | LoggerFactory := TSimpleLoggerFactory.Create; 67 | 68 | // configure with TStrings 69 | S := TStringList.Create; 70 | try 71 | S.Values['defaultLogLevel'] := 'debug'; 72 | SimpleLogger.Configure(S); 73 | finally 74 | S.Free; 75 | end; 76 | 77 | Logger := LoggerFactory.GetLogger('simple'); 78 | CheckFalse(Logger.IsTraceEnabled, 'trace'); 79 | CheckTrue(Logger.IsDebugEnabled, 'debug'); 80 | CheckTrue(Logger.IsInfoEnabled, 'info'); 81 | CheckTrue(Logger.IsWarnEnabled, 'warn'); 82 | CheckTrue(Logger.IsErrorEnabled, 'error'); 83 | 84 | // configure key-value pair 85 | SimpleLogger.Configure('defaultLogLevel', 'info'); 86 | Logger := LoggerFactory.GetLogger('simple'); 87 | CheckFalse(Logger.IsTraceEnabled, 'trace 2'); 88 | CheckFalse(Logger.IsDebugEnabled, 'debug 2'); 89 | CheckTrue(Logger.IsInfoEnabled, 'info 2'); 90 | CheckTrue(Logger.IsWarnEnabled, 'warn 2'); 91 | CheckTrue(Logger.IsErrorEnabled, 'error 2'); 92 | end; 93 | 94 | procedure TSimpleLoggerTests.TestConfigShowDateTime; 95 | var 96 | LoggerFactory: ILoggerFactory; 97 | Logger: ILogger; 98 | begin 99 | SimpleLogger.Configure('showDateTime', 'true'); 100 | // SimpleLogger.Configure('dateTimeFormat', 'hh:nn:ss.zzz'); 101 | 102 | LoggerFactory := TSimpleLoggerFactory.Create; 103 | 104 | Logger := LoggerFactory.GetLogger('dateTime on'); 105 | Logger.Info('testShowDateTime 1'); 106 | 107 | SimpleLogger.Configure('showDateTime', 'false'); 108 | 109 | Logger := LoggerFactory.GetLogger('dateTime off'); 110 | Logger.Info('testShowDateTime 2'); 111 | end; 112 | 113 | procedure TSimpleLoggerTests.TestDebug; 114 | var 115 | LoggerFactory: ILoggerFactory; 116 | Logger: ILogger; 117 | E: EAbort; 118 | begin 119 | LoggerFactory := TSimpleLoggerFactory.Create; 120 | 121 | SimpleLogger.Configure('defaultLogLevel', 'debug'); 122 | 123 | Logger := LoggerFactory.GetLogger('simple'); 124 | 125 | Logger.Trace('simple trace msg'); 126 | Logger.Debug('simple debug msg'); 127 | Logger.Info('simple info msg'); 128 | 129 | E := EAbort.Create('simple example exception'); 130 | Logger.Debug('simple msg', E); 131 | E.Free; 132 | end; 133 | 134 | procedure TSimpleLoggerTests.TestInfo; 135 | var 136 | LoggerFactory: ILoggerFactory; 137 | Logger: ILogger; 138 | E: EAbort; 139 | begin 140 | LoggerFactory := TSimpleLoggerFactory.Create; 141 | 142 | SimpleLogger.Configure('defaultLogLevel', 'info'); 143 | 144 | Logger := LoggerFactory.GetLogger('simple'); 145 | 146 | Logger.Trace('simple trace msg'); 147 | Logger.Debug('simple debug msg'); 148 | Logger.Info('simple info msg'); 149 | 150 | E := EAbort.Create('simple example exception'); 151 | Logger.Info('simple msg', E); 152 | E.Free; 153 | end; 154 | 155 | end. 156 | -------------------------------------------------------------------------------- /src/test/StringsLoggerTests.pas: -------------------------------------------------------------------------------- 1 | (* 2 | Copyright 2016 Michael Justin 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | *) 16 | 17 | unit StringsLoggerTests; 18 | 19 | interface 20 | 21 | uses 22 | {$IFDEF FPC} 23 | fpcunit,testregistry 24 | {$ELSE} 25 | TestFramework 26 | {$ENDIF}; 27 | 28 | type 29 | 30 | { TStringsLoggerTests } 31 | 32 | TStringsLoggerTests = class(TTestCase) 33 | published 34 | procedure CreateLogger; 35 | procedure TestInfo; 36 | end; 37 | 38 | implementation 39 | 40 | uses 41 | djLogAPI, StringsLogger, Classes, SysUtils; 42 | 43 | { TStringsLoggerTests } 44 | 45 | procedure TStringsLoggerTests.CreateLogger; 46 | var 47 | LoggerFactory: ILoggerFactory; 48 | Logger: ILogger; 49 | SB: TStringBuilder; 50 | begin 51 | SB := TStringBuilder.Create; 52 | try 53 | LoggerFactory := TStringsLoggerFactory.Create(SB); 54 | Logger := LoggerFactory.GetLogger('strings'); 55 | 56 | CheckEquals('strings', Logger.Name); 57 | finally 58 | SB.Free; 59 | end; 60 | end; 61 | 62 | procedure TStringsLoggerTests.TestInfo; 63 | var 64 | LoggerFactory: ILoggerFactory; 65 | Logger: ILogger; 66 | SB: TStringBuilder; 67 | SL: TStrings; 68 | begin 69 | SB := TStringBuilder.Create; 70 | try 71 | LoggerFactory := TStringsLoggerFactory.Create(SB); 72 | StringsLogger.Configure('defaultLogLevel', 'debug'); 73 | 74 | Logger := LoggerFactory.GetLogger('test.stringslogger'); 75 | Logger.Info('simple info msg'); 76 | Logger.Debug('simple debug msg'); 77 | try 78 | raise Exception.Create('some exception occured'); 79 | except 80 | on E: Exception do 81 | begin 82 | Logger.Error('exception', E); 83 | end; 84 | end; 85 | 86 | SL := TStringList.Create; 87 | try 88 | SL.Text := SB.ToString; 89 | 90 | CheckEquals('INFO test.stringslogger - simple info msg', SL[0]); 91 | CheckEquals('DEBUG test.stringslogger - simple debug msg', SL[1]); 92 | CheckEquals('ERROR test.stringslogger - exception', SL[2]); 93 | CheckEquals(' Exception', SL[3]); 94 | CheckEquals(' some exception occured', SL[4]); 95 | finally 96 | SL.Free; 97 | end; 98 | finally 99 | SB.Free; 100 | end; 101 | end; 102 | 103 | end. 104 | 105 | -------------------------------------------------------------------------------- /src/test/Unittests.dpr: -------------------------------------------------------------------------------- 1 | program Unittests; 2 | 3 | {$APPTYPE CONSOLE} 4 | 5 | uses 6 | djLogAPI in '..\main\djLogAPI.pas', 7 | djLoggerFactory in '..\main\djLoggerFactory.pas', 8 | djLogOverNOPLogger in '..\main\djLogOverNOPLogger.pas', 9 | LoggerFactoryTests in 'LoggerFactoryTests.pas', 10 | NOPLoggerTests in 'NOPLoggerTests.pas', 11 | SimpleLoggerTests in 'SimpleLoggerTests.pas', 12 | StringsLoggerTests in 'StringsLoggerTests.pas', 13 | Log4DLoggerTests in 'Log4DLoggerTests.pas', 14 | Log4D, 15 | TestFramework, 16 | GUITestRunner, 17 | TextTestRunner, 18 | SysUtils; 19 | 20 | begin 21 | RegisterTests('TdjLoggerFactory Tests', [TdjLoggerFactoryTests.Suite]); 22 | RegisterTests('TNOPLogger Tests', [TNOPLoggerTests.Suite]); 23 | RegisterTests('TSimpleLogger Tests', [TSimpleLoggerTests.Suite]); 24 | RegisterTests('TStringsLogger Tests', [TStringsLoggerTests.Suite]); 25 | RegisterTests('TLog4DLogger Tests', [TLog4DLoggerTests.Suite]); 26 | 27 | // Create a default ODS logger 28 | TLogBasicConfigurator.Configure; 29 | // see output in the 'Event log' IDE Window 30 | TLogLogger.GetRootLogger.Level := Debug; 31 | 32 | if FindCmdLineSwitch('text-mode', ['-', '/'], True) then 33 | begin 34 | TextTestRunner.RunRegisteredTests(rxbContinue) 35 | end 36 | else 37 | begin 38 | ReportMemoryLeaksOnShutDown := True; 39 | TGUITestRunner.RunRegisteredTests; 40 | end; 41 | 42 | ReportMemoryLeaksOnShutdown := True; 43 | end. 44 | -------------------------------------------------------------------------------- /src/test/Unittests.lpi: -------------------------------------------------------------------------------- 1 | <?xml version="1.0" encoding="UTF-8"?> 2 | <CONFIG> 3 | <ProjectOptions> 4 | <Version Value="11"/> 5 | <PathDelim Value="\"/> 6 | <General> 7 | <SessionStorage Value="InProjectDir"/> 8 | <MainUnit Value="0"/> 9 | <Title Value="Unittests"/> 10 | <ResourceType Value="res"/> 11 | <UseXPManifest Value="True"/> 12 | </General> 13 | <i18n> 14 | <EnableI18N LFM="False"/> 15 | </i18n> 16 | <BuildModes Count="1"> 17 | <Item1 Name="Default" Default="True"/> 18 | </BuildModes> 19 | <PublishOptions> 20 | <Version Value="2"/> 21 | </PublishOptions> 22 | <RunParams> 23 | <FormatVersion Value="2"/> 24 | <Modes Count="1"> 25 | <Mode0 Name="default"/> 26 | </Modes> 27 | </RunParams> 28 | <RequiredPackages Count="3"> 29 | <Item1> 30 | <PackageName Value="FPCUnitTestRunner"/> 31 | </Item1> 32 | <Item2> 33 | <PackageName Value="LCL"/> 34 | </Item2> 35 | <Item3> 36 | <PackageName Value="FCL"/> 37 | </Item3> 38 | </RequiredPackages> 39 | <Units Count="5"> 40 | <Unit0> 41 | <Filename Value="Unittests.lpr"/> 42 | <IsPartOfProject Value="True"/> 43 | </Unit0> 44 | <Unit1> 45 | <Filename Value="LoggerFactoryTests.pas"/> 46 | <IsPartOfProject Value="True"/> 47 | </Unit1> 48 | <Unit2> 49 | <Filename Value="Log4DLoggerTests.pas"/> 50 | <IsPartOfProject Value="True"/> 51 | </Unit2> 52 | <Unit3> 53 | <Filename Value="LazLoggerTests.pas"/> 54 | <IsPartOfProject Value="True"/> 55 | </Unit3> 56 | <Unit4> 57 | <Filename Value="stringbuilderloggertests.pas"/> 58 | <IsPartOfProject Value="True"/> 59 | <UnitName Value="StringBuilderLoggerTests"/> 60 | </Unit4> 61 | </Units> 62 | </ProjectOptions> 63 | <CompilerOptions> 64 | <Version Value="11"/> 65 | <PathDelim Value="\"/> 66 | <SearchPaths> 67 | <IncludeFiles Value="$(ProjOutDir)"/> 68 | <OtherUnitFiles Value="..\..\..\log4d\src;..\main"/> 69 | </SearchPaths> 70 | <Parsing> 71 | <SyntaxOptions> 72 | <SyntaxMode Value="Delphi"/> 73 | <UseAnsiStrings Value="False"/> 74 | </SyntaxOptions> 75 | </Parsing> 76 | <CodeGeneration> 77 | <Checks> 78 | <IOChecks Value="True"/> 79 | <RangeChecks Value="True"/> 80 | <OverflowChecks Value="True"/> 81 | <StackChecks Value="True"/> 82 | </Checks> 83 | </CodeGeneration> 84 | <Linking> 85 | <Debugging> 86 | <GenerateDebugInfo Value="False"/> 87 | <DebugInfoType Value="dsDwarf3"/> 88 | <UseHeaptrc Value="True"/> 89 | </Debugging> 90 | </Linking> 91 | <Other> 92 | <CompilerMessages> 93 | <IgnoredMessages idx11030="True"/> 94 | </CompilerMessages> 95 | </Other> 96 | </CompilerOptions> 97 | <Debugging> 98 | <Exceptions Count="4"> 99 | <Item1> 100 | <Name Value="EAbort"/> 101 | </Item1> 102 | <Item2> 103 | <Name Value="ECodetoolError"/> 104 | </Item2> 105 | <Item3> 106 | <Name Value="EFOpenError"/> 107 | </Item3> 108 | <Item4> 109 | <Name Value="EAssertionFailedError"/> 110 | </Item4> 111 | </Exceptions> 112 | </Debugging> 113 | </CONFIG> 114 | -------------------------------------------------------------------------------- /src/test/Unittests.lpr: -------------------------------------------------------------------------------- 1 | program Unittests; 2 | 3 | {$mode DELPHI}{$H+} 4 | 5 | uses 6 | LoggerFactoryTests, 7 | Log4DLoggerTests, 8 | NOPLoggerTests, 9 | SimpleLoggerTests, 10 | LazLoggerTests, 11 | StringsLoggerTests, 12 | Log4D, 13 | Interfaces, Forms, 14 | fpcunit, testregistry, GuiTestRunner, 15 | SysUtils; 16 | 17 | {$R *.res} 18 | 19 | var 20 | Tests: TTestSuite; 21 | 22 | begin 23 | Tests := TTestSuite.Create('Library Tests'); 24 | 25 | Tests.AddTest(TdjLoggerFactoryTests.Suite); 26 | Tests.AddTest(TLazLoggerTests.Suite); 27 | Tests.AddTest(TNOPLoggerTests.Suite); 28 | Tests.AddTest(TSimpleLoggerTests.Suite); 29 | Tests.AddTest(TStringsLoggerTests.Suite); 30 | 31 | // Log4D specific initialization: create a default logger 32 | TLogBasicConfigurator.Configure; 33 | Tests.AddTest(TLog4DLoggerTests.Suite); 34 | 35 | RegisterTest('', Tests); 36 | 37 | Application.Initialize; 38 | Application.CreateForm(TGuiTestRunner, TestRunner); 39 | TestRunner.Caption := 'Logging Facade FPCUnit tests'; 40 | TestRunner.TestTree.Items[0].Text := 'Logging Facade FPCUnit tests'; 41 | Application.Run; 42 | 43 | SetHeapTraceOutput('heaptrace.log'); 44 | end. 45 | 46 | --------------------------------------------------------------------------------