├── .github
└── workflows
│ └── codeql-analysis.yml
├── .gitignore
├── .travis.yml
├── License.txt
├── README.md
├── color-logger-jdk
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── mihnita
│ │ │ └── colorlog
│ │ │ └── jdk
│ │ │ ├── AnsiColorConsoleHandler.java
│ │ │ ├── BaseColorConsoleHandler.java
│ │ │ └── JAnsiColorConsoleHandler.java
│ └── resources
│ │ ├── Copyright.txt
│ │ └── License.txt
│ └── test
│ ├── java
│ ├── ColorBaseTest.java
│ └── ColorJdkTest.java
│ └── resources
│ ├── logJdk.properties
│ ├── logJdkColorEsc.properties
│ ├── logJdkColorJansi.properties
│ └── logJdkNormal.properties
├── color-logger-log4j
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── mihnita
│ │ │ └── colorlog
│ │ │ └── log4j
│ │ │ ├── AnsiColorConsoleAppender.java
│ │ │ ├── BaseColorConsoleAppender.java
│ │ │ └── JAnsiColorConsoleAppender.java
│ └── resources
│ │ ├── Copyright.txt
│ │ └── License.txt
│ └── test
│ ├── java
│ ├── ColorBaseTest.java
│ ├── ColorLog4jTest.java
│ └── ColorLog4jXmlTest.java
│ └── resources
│ ├── log4jPropColorEsc.properties
│ ├── log4jPropColorEscRegion.properties
│ ├── log4jPropColorJansi.properties
│ ├── log4jPropColorJansiRegion.properties
│ ├── log4jPropNormal.properties
│ ├── log4jXmlColorEsc.xml
│ ├── log4jXmlColorEscRegion.xml
│ ├── log4jXmlColorJansi.xml
│ ├── log4jXmlColorJansiRegion.xml
│ └── log4jXmlNormal.xml
├── color-logger-logback
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── mihnita
│ │ │ └── colorlog
│ │ │ └── logback
│ │ │ ├── ColorPatternLayoutEncoder.java
│ │ │ └── CustomHighlightingCompositeConverter.java
│ └── resources
│ │ ├── Copyright.txt
│ │ └── License.txt
│ └── test
│ ├── java
│ ├── ColorBaseTest.java
│ └── ColorLogbackTest.java
│ └── resources
│ ├── logbackColorEsc.xml
│ ├── logbackColorEscRegion.xml
│ ├── logbackNormal.xml
│ └── logbackNormalRegion.xml
├── pom.xml
└── superpom
└── pom.xml
/.github/workflows/codeql-analysis.yml:
--------------------------------------------------------------------------------
1 | # For most projects, this workflow file will not need changing; you simply need
2 | # to commit it to your repository.
3 | #
4 | # You may wish to alter this file to override the set of languages analyzed,
5 | # or to provide custom queries or build logic.
6 | #
7 | # ******** NOTE ********
8 | # We have attempted to detect the languages in your repository. Please check
9 | # the `language` matrix defined below to confirm you have the correct set of
10 | # supported CodeQL languages.
11 | #
12 | name: "CodeQL"
13 |
14 | on:
15 | push:
16 | branches: [ main ]
17 | pull_request:
18 | # The branches below must be a subset of the branches above
19 | branches: [ main ]
20 | schedule:
21 | - cron: '43 7 * * 0'
22 |
23 | jobs:
24 | analyze:
25 | name: Analyze
26 | runs-on: ubuntu-latest
27 |
28 | strategy:
29 | fail-fast: false
30 | matrix:
31 | language: [ 'java' ]
32 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
33 | # Learn more:
34 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
35 |
36 | steps:
37 | - name: Checkout repository
38 | uses: actions/checkout@v2
39 |
40 | # Initializes the CodeQL tools for scanning.
41 | - name: Initialize CodeQL
42 | uses: github/codeql-action/init@v1
43 | with:
44 | languages: ${{ matrix.language }}
45 | # If you wish to specify custom queries, you can do so here or in a config file.
46 | # By default, queries listed here will override any specified in a config file.
47 | # Prefix the list here with "+" to use these queries and those in the config file.
48 | # queries: ./path/to/local/query, your-org/your-repo/queries@main
49 |
50 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
51 | # If this step fails, then you should remove it and run the build manually (see below)
52 | - name: Autobuild
53 | uses: github/codeql-action/autobuild@v1
54 |
55 | # ℹ️ Command-line programs to run using the OS shell.
56 | # 📚 https://git.io/JvXDl
57 |
58 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
59 | # and modify them (or add more) to build your code if your project
60 | # uses a compiled language
61 |
62 | #- run: |
63 | # make bootstrap
64 | # make release
65 |
66 | - name: Perform CodeQL Analysis
67 | uses: github/codeql-action/analyze@v1
68 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | target/
2 | .idea/
3 | .settings/
4 | .classpath
5 | .project
6 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | dist: trusty
3 | jdk:
4 | - openjdk7
5 | - oraclejdk8
6 | - openjdk8
7 | - openjdk9
8 | - oraclejdk9
9 | - openjdk10
10 | - openjdk11
11 | - oraclejdk11
12 |
--------------------------------------------------------------------------------
/License.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright 2012-2013 Mihai Nita
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | java-color-loggers
2 | ==================
3 |
4 | Color console logging for log4j and jdk
5 |
6 | ## Usage
7 |
8 | * Download the latest color-loggers jar from [releases](https://github.com/mihnita/java-color-loggers/releases) or build them yourself
9 | * Download [jansi-1.18.jar](https://fusesource.github.io/jansi/download.html) - optional, see below
10 | (`jansi-1.18` is the latest one at this time, but I use nothing fancy, so I don't expect surprises with other versions)
11 | * Make sure the jar(s) mentioned above are somewhere in the `classpath` (whatever method you are using for your project)
12 |
13 | On Mac OS X, Linux, UNIX, most (all?) the consoles support ANSI escapes.
14 |
15 | ANSI escapes work out of the box in Windows 10.
16 |
17 | On older Windows versions you can use the [ConEmu console](https://conemu.github.io/),
18 | or the standard Windows console with [ansicon](https://github.com/adoxa/ansicon/downloads/)
19 | and the `AnsiColorConsole*` flavors of the loggers.
20 | But if you want the standard Windows console without `ansicon`,
21 | then you should use `JAnsiColorConsole*` and you also need `jansi`.
22 |
23 | There are pros and cons for each option, I hope to write an article when I have some time.
24 |
25 | ---
26 |
27 | ### JDK (or JUL, `java.util.logging`)
28 |
29 | In the java logging configuration file (default in `$JAVA_HOME/lib/logging.properties`)
30 | use `com.mihnita.colorlog.jdk.AnsiColorConsoleHandler`
31 | or `com.mihnita.colorlog.jdk.JAnsiColorConsoleHandler` as handler.
32 |
33 | They work the same as `java.util.logging.ConsoleHandler`.
34 |
35 | See an example in `src/test/resources/logJdk.properties`
36 |
37 | ---
38 |
39 | ### Log4j
40 |
41 | In `log4j.properties` (or whatever config file you use for log4j)
42 | use `com.mihnita.colorlog.log4j.AnsiColorConsoleAppender`
43 | or `com.mihnita.colorlog.log4j.JAnsiColorConsoleAppender` as appender.
44 |
45 | They work the same as `org.apache.log4j.ConsoleAppender`.
46 |
47 | See an example in `src/test/resources/log4j.properties` or `src/test/resources/log4j.xml`
48 |
49 | ---
50 |
51 | ### Logback
52 |
53 | Logback already allows for colored output using jansi, but does not allow configuring the colors.
54 | But now, with `java-color-loggers`, you can.
55 |
56 | In `logback.xml` (or whatever config file you use for Logback) find the appender that
57 | uses `ch.qos.logback.core.ConsoleAppender`, find the `encoder` element, and add a `class`
58 | attribute with the value of `com.mihnita.colorlog.logback.ColorPatternLayoutEncoder`.
59 |
60 | And as children of the `encoder` element you can add the following elements to configure the colors:
61 | ```
62 | 1;31
63 | 1;33
64 | 32
65 | 36
66 | 1;30
67 | ```
68 |
69 | The values should be valid ANSI escape sequences, without the `{esc}[` and without the final `m`.
70 |
71 | See an example in `src/test/resources/logback.xml`
72 |
73 | ---
74 |
75 | ### Log4j 2
76 |
77 | Log4j 2 supports colors out of the box, see
78 | https://logging.apache.org/log4j/2.x/manual/layouts.html#enable-jansi
79 |
80 | ---
81 |
82 | ### SLF4J
83 |
84 | Nothing special about it, you configure the logger “underneath it”
85 | (`jdk` or `log4j` or `logback`).
86 |
87 | ---
88 |
89 | ## Status
90 |
91 | **Build:** [](https://travis-ci.org/mihnita/java-color-loggers)
92 |
93 | **DepShield:** 
94 |
95 | ---
96 |
97 | ## Building
98 |
99 | ```
100 | mvn clean install
101 | ```
102 |
103 | But `maven` (or maybe `junit`?) “hijacks” the output of the test.
104 | So not all the lines that would normally be colored are colored in the `maven` test.
105 | The best way to try things is to run the tests in `ColorLogTest.java` as a normal
106 | application against the final jar (with the 3rd party jars in “the right place”).
107 |
--------------------------------------------------------------------------------
/color-logger-jdk/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | com.mihnita
6 | color-loggers
7 | 1.0.8-SNAPSHOT
8 |
9 |
10 | color-logger-jdk
11 | Color Loggers - JDK
12 | A logging extensions that adds level-based color to java.util.Logging (JUL).
13 |
14 |
15 |
16 | org.fusesource.jansi
17 | jansi
18 |
19 |
20 |
21 | junit
22 | junit
23 | test
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/main/java/com/mihnita/colorlog/jdk/AnsiColorConsoleHandler.java:
--------------------------------------------------------------------------------
1 | package com.mihnita.colorlog.jdk;
2 |
3 | import java.util.logging.LogRecord;
4 |
5 | /** Color Console Handler for jdk: using ANSI sequences directly (no dependencies) */
6 | public class AnsiColorConsoleHandler extends BaseColorConsoleHandler {
7 | @Override
8 | public void publish(LogRecord record) {
9 | System.err.print(logRecordToString(record));
10 | System.err.flush();
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/main/java/com/mihnita/colorlog/jdk/BaseColorConsoleHandler.java:
--------------------------------------------------------------------------------
1 | package com.mihnita.colorlog.jdk;
2 |
3 | import java.util.logging.ConsoleHandler;
4 | import java.util.logging.Formatter;
5 | import java.util.logging.Level;
6 | import java.util.logging.LogRecord;
7 |
8 | /** Color Console Handler for jdk: using ANSI sequences */
9 | abstract class BaseColorConsoleHandler extends ConsoleHandler {
10 | private static final String COLOR_RESET = "\u001b[0m";
11 |
12 | private static final String COLOR_SEVERE = "\u001b[91m";
13 | private static final String COLOR_WARNING = "\u001b[93m";
14 | private static final String COLOR_INFO = "\u001b[32m";
15 | private static final String COLOR_CONFIG = "\u001b[94m";
16 | private static final String COLOR_FINE = "\u001b[36m";
17 | private static final String COLOR_FINER = "\u001b[35m";
18 | private static final String COLOR_FINEST = "\u001b[90m";
19 |
20 | String logRecordToString(LogRecord record) {
21 | Formatter f = getFormatter();
22 | String msg = f.format(record);
23 |
24 | String prefix;
25 | Level level = record.getLevel();
26 | if (level == Level.SEVERE) {
27 | prefix = COLOR_SEVERE;
28 | } else if (level == Level.WARNING) {
29 | prefix = COLOR_WARNING;
30 | } else if (level == Level.INFO) {
31 | prefix = COLOR_INFO;
32 | } else if (level == Level.CONFIG) {
33 | prefix = COLOR_CONFIG;
34 | } else if (level == Level.FINE) {
35 | prefix = COLOR_FINE;
36 | } else if (level == Level.FINER) {
37 | prefix = COLOR_FINER;
38 | } else if (level == Level.FINEST) {
39 | prefix = COLOR_FINEST;
40 | } else {
41 | // Unknown level, probably not possible, but if it happens it means it's bad :-)
42 | prefix = COLOR_SEVERE;
43 | }
44 |
45 | return prefix + msg + COLOR_RESET;
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/main/java/com/mihnita/colorlog/jdk/JAnsiColorConsoleHandler.java:
--------------------------------------------------------------------------------
1 | package com.mihnita.colorlog.jdk;
2 |
3 | import org.fusesource.jansi.AnsiConsole;
4 | import org.fusesource.jansi.AnsiPrintStream;
5 |
6 | import java.util.logging.LogRecord;
7 |
8 | @SuppressWarnings("javadoc")
9 | public class JAnsiColorConsoleHandler extends BaseColorConsoleHandler {
10 | @Override
11 | public void publish(LogRecord record) {
12 | try (AnsiPrintStream err = AnsiConsole.err()) {
13 | err.print(logRecordToString(record));
14 | err.flush();
15 | }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/main/resources/Copyright.txt:
--------------------------------------------------------------------------------
1 | Color console logging for log4j and jdk
2 | Copyright 2012 Mihai Nita
3 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/main/resources/License.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright 1999-2005 The Apache Software Foundation
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/test/java/ColorBaseTest.java:
--------------------------------------------------------------------------------
1 | import org.junit.runner.JUnitCore;
2 |
3 | import java.net.URISyntaxException;
4 | import java.net.URL;
5 |
6 | class ColorBaseTest {
7 |
8 | static String getTargetDir() {
9 | URL url = ColorBaseTest.class.getResource(".");
10 | if (null == url) {
11 | return ".";
12 | }
13 |
14 | try {
15 | return url.toURI().getPath();
16 | } catch (URISyntaxException e) {
17 | e.printStackTrace();
18 | }
19 |
20 | return ".";
21 | }
22 |
23 | public static void main(String[] argv) {
24 | Class>[] classesArray = {ColorJdkTest.class};
25 | JUnitCore.runClasses(classesArray);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/test/java/ColorJdkTest.java:
--------------------------------------------------------------------------------
1 | import org.junit.Test;
2 |
3 | import java.io.FileInputStream;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.util.logging.Level;
7 |
8 | @SuppressWarnings("javadoc")
9 | public class ColorJdkTest extends ColorBaseTest {
10 | private final java.util.logging.Logger logger = java.util.logging.Logger.getLogger("log-jdk");
11 |
12 | static {
13 | System.out.println("===============================");
14 | System.out.println("====== Color logger - jdk =====");
15 | System.out.println("===============================");
16 | }
17 |
18 | private void selectLoggingConfigFile(String configFileName) {
19 | try (InputStream ins = new FileInputStream(getTargetDir() + configFileName)) {
20 | java.util.logging.LogManager.getLogManager().readConfiguration(ins);
21 | } catch (IOException e) {
22 | logger.severe("Error loading custom logging configuration " + configFileName);
23 | }
24 | }
25 |
26 | private void doTheLogging(String configFileName) {
27 | selectLoggingConfigFile(configFileName);
28 |
29 | System.out.println();
30 | logger.severe("severe");
31 | logger.warning("warning");
32 | logger.info("info");
33 | logger.config("config");
34 | logger.fine("fine");
35 | logger.finer("finer");
36 | logger.finest("finest");
37 |
38 | if (!"true".equals(System.getProperty("skipExceptionTests", "false"))) {
39 | logger.log(
40 | Level.WARNING,
41 | "warning with exception",
42 | new NullPointerException("Just testing"));
43 | }
44 | }
45 |
46 | @Test
47 | public void colorLoggingJdkTest() {
48 | doTheLogging("logJdkNormal.properties");
49 | doTheLogging("logJdkColorEsc.properties");
50 | doTheLogging("logJdkColorJansi.properties");
51 | System.out.println();
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/test/resources/logJdk.properties:
--------------------------------------------------------------------------------
1 | .level = FINEST
2 |
3 | handlers = java.util.logging.ConsoleHandler, com.mihnita.colorlog.jdk.AnsiColorConsoleHandler, com.mihnita.colorlog.jdk.JAnsiColorConsoleHandler
4 | java.util.logging.ConsoleHandler.level = FINEST
5 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/test/resources/logJdkColorEsc.properties:
--------------------------------------------------------------------------------
1 | .level = FINEST
2 |
3 | handlers = com.mihnita.colorlog.jdk.AnsiColorConsoleHandler
4 | java.util.logging.ConsoleHandler.level = FINEST
5 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/test/resources/logJdkColorJansi.properties:
--------------------------------------------------------------------------------
1 | .level = FINEST
2 |
3 | handlers = com.mihnita.colorlog.jdk.JAnsiColorConsoleHandler
4 | java.util.logging.ConsoleHandler.level = FINEST
5 |
--------------------------------------------------------------------------------
/color-logger-jdk/src/test/resources/logJdkNormal.properties:
--------------------------------------------------------------------------------
1 | .level = FINEST
2 |
3 | handlers = java.util.logging.ConsoleHandler
4 | java.util.logging.ConsoleHandler.level = FINEST
5 |
--------------------------------------------------------------------------------
/color-logger-log4j/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | com.mihnita
6 | color-loggers
7 | 1.0.8-SNAPSHOT
8 |
9 |
10 | color-logger-log4j
11 | Color Loggers - Log4j
12 | A logging extension that adds level-based color to Log4j.
13 |
14 |
15 |
16 | org.fusesource.jansi
17 | jansi
18 |
19 |
20 | log4j
21 | log4j
22 |
23 |
24 |
25 | junit
26 | junit
27 | test
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/main/java/com/mihnita/colorlog/log4j/AnsiColorConsoleAppender.java:
--------------------------------------------------------------------------------
1 | package com.mihnita.colorlog.log4j;
2 |
3 | import org.apache.log4j.Layout;
4 | import org.apache.log4j.spi.LoggingEvent;
5 |
6 | /** Color Console Appender for log4j: using ANSI sequences directly (no dependencies) */
7 | @SuppressWarnings("javadoc")
8 | public class AnsiColorConsoleAppender extends BaseColorConsoleAppender {
9 |
10 | public AnsiColorConsoleAppender() {
11 | super();
12 | }
13 |
14 | public AnsiColorConsoleAppender(Layout layout) {
15 | super(layout);
16 | }
17 |
18 | public AnsiColorConsoleAppender(Layout layout, String target) {
19 | super(layout, target);
20 | }
21 |
22 | @Override
23 | protected void subAppend(LoggingEvent event) {
24 | if (!hackPatternString()) {
25 | qw.write(getColour(event.getLevel()));
26 | qw.write(getLayout().format(event));
27 | } else {
28 | String color = getColour(event.getLevel());
29 | qw.write(getLayout().format(event).replace(HIGHLIGHT_START, color));
30 | }
31 |
32 | if (immediateFlush) {
33 | qw.flush();
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/main/java/com/mihnita/colorlog/log4j/BaseColorConsoleAppender.java:
--------------------------------------------------------------------------------
1 | package com.mihnita.colorlog.log4j;
2 |
3 | import org.apache.log4j.ConsoleAppender;
4 | import org.apache.log4j.EnhancedPatternLayout;
5 | import org.apache.log4j.Layout;
6 | import org.apache.log4j.Level;
7 | import org.apache.log4j.PatternLayout;
8 |
9 | import java.util.HashMap;
10 | import java.util.Map;
11 |
12 | /** Color Console Appender for log4j: using ANSI sequences */
13 | abstract class BaseColorConsoleAppender extends ConsoleAppender {
14 | private static final String COLOR_RESET = "\u001b[0m";
15 | private static final String END_NL = "%n";
16 | private static final String END_NL_AND_THROW = "%n%throwable";
17 |
18 | private static final String HIGHLIGHT_END = "{/highlight}";
19 | static final String HIGHLIGHT_START = "{highlight}";
20 |
21 | private String gPattern = "";
22 | private boolean gPatternHighlight = false;
23 |
24 | private final Map levelToColor = new HashMap<>();
25 |
26 | {
27 | levelToColor.put(Level.FATAL, "\u001b[97;41m");
28 | levelToColor.put(Level.ERROR, "\u001b[91m");
29 | levelToColor.put(Level.WARN, "\u001b[93m");
30 | levelToColor.put(Level.INFO, "\u001b[22;32m");
31 | levelToColor.put(Level.DEBUG, "\u001b[22;36m");
32 | levelToColor.put(Level.TRACE, "\u001b[90m");
33 | }
34 |
35 | BaseColorConsoleAppender() {
36 | super();
37 | }
38 |
39 | BaseColorConsoleAppender(Layout layout) {
40 | super(layout);
41 | }
42 |
43 | BaseColorConsoleAppender(Layout layout, String target) {
44 | super(layout, target);
45 | }
46 |
47 | public void setFatalColour(String value) {
48 | levelToColor.put(Level.FATAL, value.replace("{esc}", "\u001b"));
49 | }
50 |
51 | public void setErrorColour(String value) {
52 | levelToColor.put(Level.ERROR, value.replace("{esc}", "\u001b"));
53 | }
54 |
55 | public void setWarnColour(String value) {
56 | levelToColor.put(Level.WARN, value.replace("{esc}", "\u001b"));
57 | }
58 |
59 | public void setInfoColour(String value) {
60 | levelToColor.put(Level.INFO, value.replace("{esc}", "\u001b"));
61 | }
62 |
63 | public void setDebugColour(String value) {
64 | levelToColor.put(Level.DEBUG, value.replace("{esc}", "\u001b"));
65 | }
66 |
67 | public void setTraceColour(String value) {
68 | levelToColor.put(Level.TRACE, value.replace("{esc}", "\u001b"));
69 | }
70 |
71 | String getColour(Level level) {
72 | String result = levelToColor.get(level);
73 | if (null == result) {
74 | return levelToColor.get(Level.ERROR);
75 | }
76 | return result;
77 | }
78 |
79 | /*
80 | * Adds a "reset color" before the newline to prevent some ugly artifacts
81 | */
82 | boolean hackPatternString() {
83 | EnhancedPatternLayout enhancedPatternLayout = null;
84 | PatternLayout patternLayout = null;
85 | String pattern;
86 |
87 | Class> c = this.getLayout().getClass();
88 | if (EnhancedPatternLayout.class.isAssignableFrom(c)) {
89 | enhancedPatternLayout = (EnhancedPatternLayout) this.getLayout();
90 | if (null == enhancedPatternLayout) {
91 | return gPatternHighlight;
92 | }
93 | pattern = enhancedPatternLayout.getConversionPattern();
94 | } else if (PatternLayout.class.isAssignableFrom(c)) {
95 | patternLayout = (PatternLayout) this.getLayout();
96 | if (null == patternLayout) {
97 | return gPatternHighlight;
98 | }
99 | pattern = patternLayout.getConversionPattern();
100 | } else {
101 | return gPatternHighlight;
102 | }
103 |
104 | if (gPattern.equals(pattern)) {
105 | return gPatternHighlight;
106 | }
107 |
108 | int hiStart = pattern.indexOf(HIGHLIGHT_START);
109 | gPatternHighlight = (hiStart != -1);
110 |
111 | // If we have a {/highlight}, we put the COLOR_RESET there
112 | // Otherwise we put it at the end, or right before the final %n
113 | if (pattern.contains(HIGHLIGHT_END)) {
114 | gPattern = pattern.replace(HIGHLIGHT_END, COLOR_RESET);
115 | } else if (pattern.endsWith(END_NL)) {
116 | gPattern =
117 | pattern.substring(0, pattern.length() - END_NL.length()) + COLOR_RESET + END_NL;
118 | } else if (pattern.endsWith(END_NL_AND_THROW)) {
119 | gPattern =
120 | pattern.substring(0, pattern.length() - END_NL_AND_THROW.length())
121 | + COLOR_RESET
122 | + END_NL_AND_THROW;
123 | } else {
124 | gPattern = pattern + COLOR_RESET;
125 | }
126 |
127 | if (null != enhancedPatternLayout) {
128 | enhancedPatternLayout.setConversionPattern(gPattern);
129 | this.setLayout(enhancedPatternLayout);
130 | }
131 | if (null != patternLayout) {
132 | patternLayout.setConversionPattern(gPattern);
133 | this.setLayout(patternLayout);
134 | }
135 |
136 | return gPatternHighlight;
137 | }
138 | }
139 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/main/java/com/mihnita/colorlog/log4j/JAnsiColorConsoleAppender.java:
--------------------------------------------------------------------------------
1 | package com.mihnita.colorlog.log4j;
2 |
3 | import org.apache.log4j.Layout;
4 | import org.apache.log4j.spi.LoggingEvent;
5 | import org.fusesource.jansi.AnsiConsole;
6 |
7 | import java.io.PrintStream;
8 |
9 | @SuppressWarnings("javadoc")
10 | public class JAnsiColorConsoleAppender extends BaseColorConsoleAppender {
11 | private final String gTarget = null;
12 | private boolean usingStdErr;
13 |
14 | public JAnsiColorConsoleAppender() {
15 | super();
16 | }
17 |
18 | public JAnsiColorConsoleAppender(Layout layout) {
19 | super(layout);
20 | }
21 |
22 | public JAnsiColorConsoleAppender(Layout layout, String target) {
23 | super(layout, target);
24 | }
25 |
26 | @SuppressWarnings("static-method")
27 | public void setPassThrough(boolean value) {
28 | System.setProperty("jansi.passthrough", value ? "true" : "false");
29 | }
30 |
31 | @SuppressWarnings("static-method")
32 | public void setStrip(boolean value) {
33 | System.setProperty("jansi.strip", value ? "true" : "false");
34 | }
35 |
36 | @Override
37 | protected void subAppend(LoggingEvent event) {
38 | try (@SuppressWarnings("resource") // false positive. It is in fact closed.
39 | PrintStream currentOutput = usingStdErr ? AnsiConsole.err() : AnsiConsole.out()) {
40 |
41 | if (!hackPatternString()) {
42 | currentOutput.print(getColour(event.getLevel()));
43 | currentOutput.print(getLayout().format(event));
44 | } else {
45 | String color = getColour(event.getLevel());
46 | currentOutput.print(getLayout().format(event).replace(HIGHLIGHT_START, color));
47 | }
48 |
49 | if (immediateFlush) {
50 | currentOutput.flush();
51 | }
52 | }
53 | }
54 |
55 | @Override
56 | boolean hackPatternString() {
57 | String theTarget = getTarget();
58 | // no-inspection StringEquality
59 | if (gTarget != theTarget) { // I really want to have the same object, not just equal content
60 | usingStdErr = SYSTEM_ERR.equalsIgnoreCase(theTarget);
61 | }
62 |
63 | return super.hackPatternString();
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/main/resources/Copyright.txt:
--------------------------------------------------------------------------------
1 | Color console logging for log4j and jdk
2 | Copyright 2012 Mihai Nita
3 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/main/resources/License.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright 1999-2005 The Apache Software Foundation
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/java/ColorBaseTest.java:
--------------------------------------------------------------------------------
1 | import org.junit.runner.JUnitCore;
2 |
3 | import java.net.URISyntaxException;
4 | import java.net.URL;
5 |
6 | class ColorBaseTest {
7 |
8 | static String getTargetDir() {
9 | URL url = ColorBaseTest.class.getResource(".");
10 | if (null == url) {
11 | return ".";
12 | }
13 |
14 | try {
15 | return url.toURI().getPath();
16 | } catch (URISyntaxException e) {
17 | e.printStackTrace();
18 | }
19 |
20 | return ".";
21 | }
22 |
23 | public static void main(String[] argv) {
24 | Class>[] classesArray = {ColorLog4jTest.class, ColorLog4jXmlTest.class};
25 | JUnitCore.runClasses(classesArray);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/java/ColorLog4jTest.java:
--------------------------------------------------------------------------------
1 | import org.apache.log4j.PropertyConfigurator;
2 | import org.junit.Test;
3 |
4 | @SuppressWarnings("javadoc")
5 | public class ColorLog4jTest extends ColorBaseTest {
6 | private final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger("log-log4j");
7 |
8 | static {
9 | System.out.println("===============================");
10 | System.out.println("===== Color logger - log4j ====");
11 | System.out.println("===============================");
12 | }
13 |
14 | private static void selectLoggingConfigFile(String configFileName) {
15 | PropertyConfigurator.configure(getTargetDir() + configFileName);
16 | }
17 |
18 | private void doTheLogging(String configFileName) {
19 | selectLoggingConfigFile(configFileName);
20 |
21 | System.out.println();
22 | logger.fatal("fatal");
23 | logger.error("error");
24 | logger.warn("warn");
25 | logger.info("info");
26 | logger.debug("debug");
27 | logger.trace("trace");
28 |
29 | if (!"true".equals(System.getProperty("skipExceptionTests", "false"))) {
30 | logger.warn("warn with exception", new NullPointerException("Just testing"));
31 | }
32 | }
33 |
34 | @Test
35 | public void colorLoggingLog4j() {
36 | doTheLogging("log4jPropNormal.properties");
37 | doTheLogging("log4jPropColorEsc.properties");
38 | doTheLogging("log4jPropColorEscRegion.properties");
39 | doTheLogging("log4jPropColorJansi.properties");
40 | doTheLogging("log4jPropColorJansiRegion.properties");
41 | System.out.println();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/java/ColorLog4jXmlTest.java:
--------------------------------------------------------------------------------
1 | import org.apache.log4j.xml.DOMConfigurator;
2 | import org.junit.Test;
3 |
4 | @SuppressWarnings("javadoc")
5 | public class ColorLog4jXmlTest extends ColorBaseTest {
6 | private final org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger("log-log4j");
7 |
8 | static {
9 | System.out.println("===================================================");
10 | System.out.println("===== Color logger - log4j - XML configuration ====");
11 | System.out.println("===================================================");
12 | }
13 |
14 | private static void selectLoggingConfigFile(String configFileName) {
15 | DOMConfigurator.configure(getTargetDir() + configFileName);
16 | }
17 |
18 | private void doTheLogging(String configFileName) {
19 | selectLoggingConfigFile(configFileName);
20 |
21 | System.out.println();
22 | logger.fatal("fatal with XML");
23 | logger.error("error with XML");
24 | logger.warn("warn with XML");
25 | logger.info("info with XML");
26 | logger.debug("debug with XML");
27 | logger.trace("trace with XML");
28 |
29 | if (!"true".equals(System.getProperty("skipExceptionTests", "false"))) {
30 | logger.warn("warn with XML and exception", new NullPointerException("Just testing"));
31 | }
32 | }
33 |
34 | @Test
35 | public void colorLoggingLog4j() {
36 | doTheLogging("log4jXmlNormal.xml");
37 | doTheLogging("log4jXmlColorEsc.xml");
38 | doTheLogging("log4jXmlColorEscRegion.xml");
39 | doTheLogging("log4jXmlColorJansi.xml");
40 | doTheLogging("log4jXmlColorJansiRegion.xml");
41 | System.out.println();
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/resources/log4jPropColorEsc.properties:
--------------------------------------------------------------------------------
1 | # This file gets read by log4j as part of its default configuration during startup.
2 | # See http://logging.apache.org/log4j/1.2/manual.html for details on where log4j looks
3 | # for its configuration.
4 |
5 | log4j.rootLogger=TRACE, A
6 |
7 | log4j.appender.A=com.mihnita.colorlog.log4j.AnsiColorConsoleAppender
8 | log4j.appender.A.layout=org.apache.log4j.EnhancedPatternLayout
9 | log4j.appender.A.layout.ConversionPattern=Ansi> %-5p: %c{2} [%t] - %m%n%throwable
10 |
11 | # You can change the default colors
12 | # log4j.appender.A.FatalColour={esc}[95m
13 | # log4j.appender.A.ErrorColour={esc}[31m
14 | # log4j.appender.A.WarnColour ={esc}[33m
15 | # log4j.appender.A.InfoColour ={esc}[92m
16 | # log4j.appender.A.DebugColour={esc}[96m
17 | # log4j.appender.A.TraceColour={esc}[90m
18 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/resources/log4jPropColorEscRegion.properties:
--------------------------------------------------------------------------------
1 | # This file gets read by log4j as part of its default configuration during startup.
2 | # See http://logging.apache.org/log4j/1.2/manual.html for details on where log4j looks
3 | # for its configuration.
4 |
5 | log4j.rootLogger=TRACE, A
6 |
7 | log4j.appender.A=com.mihnita.colorlog.log4j.AnsiColorConsoleAppender
8 | log4j.appender.A.layout=org.apache.log4j.EnhancedPatternLayout
9 | log4j.appender.A.layout.ConversionPattern=AnsiReg> {highlight}%-5p:{/highlight} %c{2} [%t] - %m%n%throwable
10 |
11 | # You can change the default colors
12 | # log4j.appender.A.FatalColour={esc}[95m
13 | # log4j.appender.A.ErrorColour={esc}[31m
14 | # log4j.appender.A.WarnColour ={esc}[33m
15 | # log4j.appender.A.InfoColour ={esc}[92m
16 | # log4j.appender.A.DebugColour={esc}[96m
17 | # log4j.appender.A.TraceColour={esc}[90m
18 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/resources/log4jPropColorJansi.properties:
--------------------------------------------------------------------------------
1 | # This file gets read by log4j as part of its default configuration during startup.
2 | # See http://logging.apache.org/log4j/1.2/manual.html for details on where log4j looks
3 | # for its configuration.
4 |
5 | log4j.rootLogger=TRACE, A
6 |
7 | log4j.appender.A=com.mihnita.colorlog.log4j.JAnsiColorConsoleAppender
8 | log4j.appender.A.layout=org.apache.log4j.EnhancedPatternLayout
9 | log4j.appender.A.layout.ConversionPattern=Jansi> %-5p: %c{2} [%t] - %m%n%throwable
10 |
11 | # Lets the ANSI escape sequences to pass thru, instead of calling Windows API to set color
12 | # log4j.appender.A.PassThrough=true
13 | # Remove the ANSI escape sequences
14 | # log4j.appender.A.Strip=true
15 |
16 | # You can change the default colors
17 | # log4j.appender.A.FatalColour={esc}[95m
18 | # log4j.appender.A.ErrorColour={esc}[31m
19 | # log4j.appender.A.WarnColour ={esc}[33m
20 | # log4j.appender.A.InfoColour ={esc}[92m
21 | # log4j.appender.A.DebugColour={esc}[96m
22 | # log4j.appender.A.TraceColour={esc}[90m
23 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/resources/log4jPropColorJansiRegion.properties:
--------------------------------------------------------------------------------
1 | # This file gets read by log4j as part of its default configuration during startup.
2 | # See http://logging.apache.org/log4j/1.2/manual.html for details on where log4j looks
3 | # for its configuration.
4 |
5 | log4j.rootLogger=TRACE, A
6 |
7 | log4j.appender.A=com.mihnita.colorlog.log4j.JAnsiColorConsoleAppender
8 | log4j.appender.A.layout=org.apache.log4j.EnhancedPatternLayout
9 | log4j.appender.A.layout.ConversionPattern=JansiReg> {highlight}%-5p:{/highlight} %c{2} [%t] - %m%n%throwable
10 |
11 | # Lets the ANSI escape sequences to pass thru, instead of calling Windows API to set color
12 | # log4j.appender.A.PassThrough=true
13 | # Remove the ANSI escape sequences
14 | # log4j.appender.A.Strip=true
15 |
16 | # You can change the default colors
17 | # log4j.appender.A.FatalColour={esc}[95m
18 | # log4j.appender.A.ErrorColour={esc}[31m
19 | # log4j.appender.A.WarnColour ={esc}[33m
20 | # log4j.appender.A.InfoColour ={esc}[92m
21 | # log4j.appender.A.DebugColour={esc}[96m
22 | # log4j.appender.A.TraceColour={esc}[90m
23 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/resources/log4jPropNormal.properties:
--------------------------------------------------------------------------------
1 | # This file gets read by log4j as part of its default configuration during startup.
2 | # See http://logging.apache.org/log4j/1.2/manual.html for details on where log4j looks
3 | # for its configuration.
4 |
5 | log4j.rootLogger=TRACE, A
6 |
7 | log4j.appender.A=org.apache.log4j.ConsoleAppender
8 | log4j.appender.A.layout=org.apache.log4j.EnhancedPatternLayout
9 | log4j.appender.A.layout.ConversionPattern=log4j> %-5p: %c{2} [%t] - %m%n%throwable
10 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/resources/log4jXmlColorEsc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/resources/log4jXmlColorEscRegion.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/resources/log4jXmlColorJansi.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/resources/log4jXmlColorJansiRegion.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/color-logger-log4j/src/test/resources/log4jXmlNormal.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
--------------------------------------------------------------------------------
/color-logger-logback/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | com.mihnita
6 | color-loggers
7 | 1.0.8-SNAPSHOT
8 |
9 |
10 | color-logger-logback
11 | Color Loggers - Logback
12 | A logging extension that adds level-based color to Logback.
13 |
14 |
15 | 11
16 | 11
17 |
18 |
19 |
20 |
21 | org.slf4j
22 | slf4j-api
23 |
24 |
25 | org.fusesource.jansi
26 | jansi
27 |
28 |
29 | ch.qos.logback
30 | logback-core
31 |
32 |
33 | ch.qos.logback
34 | logback-classic
35 |
36 |
37 |
38 | junit
39 | junit
40 | test
41 |
42 |
43 |
44 |
45 |
--------------------------------------------------------------------------------
/color-logger-logback/src/main/java/com/mihnita/colorlog/logback/ColorPatternLayoutEncoder.java:
--------------------------------------------------------------------------------
1 | package com.mihnita.colorlog.logback;
2 |
3 | import ch.qos.logback.classic.PatternLayout;
4 | import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
5 |
6 | /** Extending the {@link ch.qos.logback.classic.encoder.PatternLayoutEncoder} */
7 | @SuppressWarnings({"static-method", "javadoc"})
8 | public class ColorPatternLayoutEncoder extends PatternLayoutEncoder {
9 |
10 | @Override
11 | public void start() {
12 | super.start();
13 |
14 | PatternLayout patternLayout = (PatternLayout) this.layout;
15 | patternLayout.stop();
16 | patternLayout
17 | .getInstanceConverterMap()
18 | .put("highlight", CustomHighlightingCompositeConverter::new);
19 | patternLayout.start();
20 |
21 | this.layout = patternLayout;
22 | }
23 |
24 | public void setErrorColor(String value) {
25 | CustomHighlightingCompositeConverter.errorColor = value;
26 | }
27 |
28 | public void setWarningColor(String value) {
29 | CustomHighlightingCompositeConverter.warningColor = value;
30 | }
31 |
32 | public void setInfoColor(String value) {
33 | CustomHighlightingCompositeConverter.infoColor = value;
34 | }
35 |
36 | public void setDebugColor(String value) {
37 | CustomHighlightingCompositeConverter.debugColor = value;
38 | }
39 |
40 | public void setTraceColor(String value) {
41 | CustomHighlightingCompositeConverter.traceColor = value;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/color-logger-logback/src/main/java/com/mihnita/colorlog/logback/CustomHighlightingCompositeConverter.java:
--------------------------------------------------------------------------------
1 | package com.mihnita.colorlog.logback;
2 |
3 | import static ch.qos.logback.core.pattern.color.ANSIConstants.BLACK_FG;
4 | import static ch.qos.logback.core.pattern.color.ANSIConstants.BOLD;
5 | import static ch.qos.logback.core.pattern.color.ANSIConstants.CYAN_FG;
6 | import static ch.qos.logback.core.pattern.color.ANSIConstants.DEFAULT_FG;
7 | import static ch.qos.logback.core.pattern.color.ANSIConstants.GREEN_FG;
8 | import static ch.qos.logback.core.pattern.color.ANSIConstants.RED_FG;
9 | import static ch.qos.logback.core.pattern.color.ANSIConstants.YELLOW_FG;
10 |
11 | import ch.qos.logback.classic.Level;
12 | import ch.qos.logback.classic.pattern.color.HighlightingCompositeConverter;
13 | import ch.qos.logback.classic.spi.ILoggingEvent;
14 |
15 | @SuppressWarnings("javadoc")
16 | public class CustomHighlightingCompositeConverter extends HighlightingCompositeConverter {
17 | static String errorColor = BOLD + RED_FG;
18 | static String warningColor = BOLD + YELLOW_FG;
19 | static String infoColor = GREEN_FG;
20 | static String debugColor = CYAN_FG;
21 | static String traceColor = BOLD + BLACK_FG;
22 |
23 | @Override
24 | protected String getForegroundColorCode(ILoggingEvent event) {
25 | Level level = event.getLevel();
26 | switch (level.toInt()) {
27 | case Level.ERROR_INT:
28 | return errorColor;
29 | case Level.WARN_INT:
30 | return warningColor;
31 | case Level.INFO_INT:
32 | return infoColor;
33 | case Level.DEBUG_INT:
34 | return debugColor;
35 | case Level.TRACE_INT:
36 | return traceColor;
37 | default:
38 | return DEFAULT_FG;
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/color-logger-logback/src/main/resources/Copyright.txt:
--------------------------------------------------------------------------------
1 | Color console logging for log4j and jdk
2 | Copyright 2012 Mihai Nita
3 |
--------------------------------------------------------------------------------
/color-logger-logback/src/main/resources/License.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright 1999-2005 The Apache Software Foundation
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/color-logger-logback/src/test/java/ColorBaseTest.java:
--------------------------------------------------------------------------------
1 | import org.junit.runner.JUnitCore;
2 |
3 | import java.net.URISyntaxException;
4 | import java.net.URL;
5 |
6 | class ColorBaseTest {
7 |
8 | static String getTargetDir() {
9 | URL url = ColorBaseTest.class.getResource(".");
10 | if (null == url) {
11 | return ".";
12 | }
13 |
14 | try {
15 | return url.toURI().getPath();
16 | } catch (URISyntaxException e) {
17 | e.printStackTrace();
18 | }
19 |
20 | return ".";
21 | }
22 |
23 | public static void main(String[] argv) {
24 | Class>[] classesArray = {ColorLogbackTest.class};
25 | JUnitCore.runClasses(classesArray);
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/color-logger-logback/src/test/java/ColorLogbackTest.java:
--------------------------------------------------------------------------------
1 | import ch.qos.logback.classic.LoggerContext;
2 | import ch.qos.logback.classic.joran.JoranConfigurator;
3 | import ch.qos.logback.core.joran.spi.JoranException;
4 | import ch.qos.logback.core.util.StatusPrinter2;
5 |
6 | import org.junit.Test;
7 |
8 | @SuppressWarnings("javadoc")
9 | public class ColorLogbackTest extends ColorBaseTest {
10 | private final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger("log-logback");
11 |
12 | static {
13 | System.out.println("===============================");
14 | System.out.println("==== Color logger - logback ===");
15 | System.out.println("===============================");
16 | }
17 |
18 | private static void selectLoggingConfigFile(String configFileName) {
19 | LoggerContext context = (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();
20 |
21 | try {
22 | JoranConfigurator configurator = new JoranConfigurator();
23 | configurator.setContext(context);
24 | context.reset();
25 | configurator.doConfigure(getTargetDir() + configFileName);
26 | } catch (JoranException e) {
27 | // StatusPrinter will handle this
28 | }
29 | new StatusPrinter2().printInCaseOfErrorsOrWarnings(context);
30 | }
31 |
32 | private void doTheLogging(String configFileName) {
33 | selectLoggingConfigFile(configFileName);
34 |
35 | System.out.println();
36 | logger.error("error");
37 | logger.warn("warn");
38 | logger.info("info");
39 | logger.debug("debug");
40 | logger.trace("trace");
41 |
42 | if (!"true".equals(System.getProperty("skipExceptionTests", "false"))) {
43 | logger.warn("warn with exception", new NullPointerException("Just testing"));
44 | }
45 | }
46 |
47 | @Test
48 | public void colorLoggingLogback() {
49 | doTheLogging("logbackNormal.xml");
50 | doTheLogging("logbackNormalRegion.xml");
51 | doTheLogging("logbackColorEsc.xml");
52 | doTheLogging("logbackColorEscRegion.xml");
53 | System.out.println();
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/color-logger-logback/src/test/resources/logbackColorEsc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | %highlight(CustomColors> %-5level: %logger{15} - %msg) %n
6 |
13 |
14 | false
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/color-logger-logback/src/test/resources/logbackColorEscRegion.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CustomColorsReg> %highlight(%-5level:) %logger{15} - %msg%n
6 |
13 |
14 | false
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/color-logger-logback/src/test/resources/logbackNormal.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | %highlight(LogbackColors> %-5level: %logger{15} - %msg) %n
6 |
7 | false
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/color-logger-logback/src/test/resources/logbackNormalRegion.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | LogbackColorsReg> %highlight(%-5level:) %logger{15} - %msg%n
6 |
7 | false
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 |
6 | com.mihnita
7 | color-loggers-superpom
8 | 1.0.8-SNAPSHOT
9 | superpom/pom.xml
10 |
11 |
12 | color-loggers
13 | pom
14 | Color Loggers
15 | A colection of logging extensions that add level-based color.
16 | https://github.com/mihnita/java-color-loggers
17 |
18 |
19 | superpom
20 | color-logger-jdk
21 | color-logger-log4j
22 | color-logger-logback
23 |
24 |
25 |
--------------------------------------------------------------------------------
/superpom/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 | com.mihnita
6 | color-loggers-superpom
7 | 1.0.8-SNAPSHOT
8 | pom
9 |
10 | Color Loggers SuperPOM
11 | A colection of logging extensions that add level-based color.
12 | https://github.com/mihnita/java-color-loggers
13 |
14 |
15 |
16 | The Apache License, Version 2.0
17 | http://www.apache.org/licenses/LICENSE-2.0.txt
18 | repo
19 |
20 |
21 |
22 |
23 |
24 | Mihai Nita
25 | java@mihnita.com
26 | No organization
27 | https://github.com/mihnita
28 |
29 |
30 |
31 |
32 | scm:git:git://github.com/mihnita/java-color-loggers.git
33 | scm:git:git://github.com:mihnita/java-color-loggers.git
34 | https://github.com/mihnita/java-color-loggers
35 |
36 |
37 |
38 | GitHub
39 | https://github.com/mihnita/java-color-loggers/issues
40 |
41 |
42 |
43 |
44 | sonatype
45 | Sonatype Releases
46 | ${url_sonatype_releases}
47 |
48 |
49 | sonatype
50 | Sonatype Snapshots
51 | ${url_sonatype_snapshots}
52 |
53 |
54 |
55 |
56 | UTF-8
57 |
59 | 1.8
60 | 1.8
61 |
62 |
63 | https://oss.sonatype.org
64 | ${staging_maven_plugin_nexusUrl}/service/local/staging/deploy/maven2/
65 | ${staging_maven_plugin_nexusUrl}/content/repositories/snapshots
66 |
67 |
68 | [3.6.3,4.0.0)
69 |
70 |
71 | 2.0.17
72 | 2.4.2
73 | 1.2.17
74 | 1.5.18
75 | 4.13.2
76 |
77 |
78 | 3.4.1
79 | 3.14.0
80 | 3.1.4
81 | 3.5.0
82 | 3.2.7
83 | 3.1.4
84 | 3.4.2
85 | 3.1.0
86 | 3.11.2
87 | 3.3.1
88 | 3.21.0
89 | 3.3.1
90 | 3.5.3
91 | 1.7.0
92 |
93 |
94 |
95 |
96 |
97 |
98 | org.slf4j
99 | slf4j-api
100 | ${org.slf4j.version}
101 |
102 |
103 | log4j
104 | log4j
105 | ${org.log4j.version}
106 |
107 |
108 | ch.qos.logback
109 | logback-core
110 | ${org.logback.version}
111 |
112 |
113 | ch.qos.logback
114 | logback-classic
115 | ${org.logback.version}
116 |
117 |
118 | org.fusesource.jansi
119 | jansi
120 | ${org.jansi.version}
121 |
122 |
123 |
124 | junit
125 | junit
126 | ${org.junit.version}
127 | test
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 | org.apache.maven.plugins
137 | maven-enforcer-plugin
138 | ${maven-enforcer-plugin.version}
139 |
140 |
141 | enforce-maven
142 |
143 | enforce
144 |
145 |
146 |
147 |
148 | ${required.maven.version}
149 |
150 |
151 |
152 |
153 |
154 |
155 |
156 | org.apache.maven.plugins
157 | maven-compiler-plugin
158 | ${maven-compiler-plugin.version}
159 |
160 |
161 | org.apache.maven.plugins
162 | maven-clean-plugin
163 | ${maven-clean-plugin.version}
164 |
165 |
166 | org.apache.maven.plugins
167 | maven-surefire-plugin
168 | ${maven-surefire-plugin.version}
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 | org.apache.maven.plugins
177 | maven-deploy-plugin
178 | ${maven-deploy-plugin.version}
179 |
180 | true
181 |
182 |
183 |
184 | org.sonatype.plugins
185 | nexus-staging-maven-plugin
186 | ${nexus-staging-maven-plugin.version}
187 | true
188 |
189 | sonatype
190 | ${staging_maven_plugin_nexusUrl}/
191 |
192 | false
193 |
194 |
195 |
196 | default-deploy
197 |
198 | deploy
199 |
200 | deploy
201 |
202 |
203 |
204 |
205 | org.apache.maven.plugins
206 | maven-install-plugin
207 | ${maven-install-plugin.version}
208 |
209 |
210 | org.apache.maven.plugins
211 | maven-jar-plugin
212 | ${maven-jar-plugin.version}
213 |
214 | ${basedir}\target
215 |
216 |
217 |
218 |
219 | test-jar
220 |
221 |
222 |
223 |
224 |
225 | org.apache.maven.plugins
226 | maven-resources-plugin
227 | ${maven-resources-plugin.version}
228 |
229 |
230 | org.apache.maven.plugins
231 | maven-site-plugin
232 | ${maven-site-plugin.version}
233 |
234 |
235 | org.apache.maven.plugins
236 | maven-source-plugin
237 | ${maven-source-plugin.version}
238 |
239 |
240 | attach-sources
241 |
242 | jar-no-fork
243 |
244 |
245 |
246 |
247 |
248 | org.apache.maven.plugins
249 | maven-javadoc-plugin
250 | ${maven-javadoc-plugin.version}
251 |
252 |
253 | attach-javadocs
254 |
255 | jar
256 |
257 |
258 |
259 |
260 |
261 | org.apache.maven.plugins
262 | maven-jarsigner-plugin
263 | ${maven-jarsigner-plugin.version}
264 |
265 | http://timestamp.comodoca.com/rfc3161
266 |
267 |
268 |
269 | sign
270 |
271 | sign
272 |
273 |
274 |
275 |
276 |
277 | org.apache.maven.plugins
278 | maven-gpg-plugin
279 | ${maven-gpg-plugin.version}
280 |
281 |
282 | sign-artifacts
283 |
284 |
285 | sign
286 |
287 |
288 | true
289 | ${gpg.keyname}
290 | ${gpg.passphrase}
291 | never
292 |
293 | --no-random-seed-file
294 | --no-permission-warning
295 |
296 |
297 |
298 |
299 |
300 |
301 |
302 |
303 |
304 |
305 | maven-enforcer-plugin
306 |
307 |
308 |
309 |
310 |
311 |
312 |
313 |
314 | release
315 |
316 |
317 |
318 | org.apache.maven.plugins
319 | maven-source-plugin
320 |
321 |
322 | org.apache.maven.plugins
323 | maven-javadoc-plugin
324 |
325 |
326 | org.apache.maven.plugins
327 | maven-jarsigner-plugin
328 |
329 |
330 | org.apache.maven.plugins
331 | maven-gpg-plugin
332 |
333 |
334 | sign-artifacts
335 |
336 | false
337 |
338 |
339 |
340 |
341 |
342 |
343 |
344 |
345 | sonatype_deploy
346 |
347 |
348 |
349 | org.sonatype.plugins
350 | nexus-staging-maven-plugin
351 |
352 |
353 |
354 |
355 |
356 |
357 |
358 |
--------------------------------------------------------------------------------