├── .classpath ├── .gitignore ├── .project ├── LICENSE ├── README.md ├── pom.xml └── src ├── main └── java │ └── com │ └── eclecticlogic │ └── whisper │ ├── core │ ├── Digest.java │ ├── DigestMessage.java │ ├── Muffler.java │ ├── ParameterUtil.java │ ├── SuppressionQueue.java │ └── WhisperManager.java │ ├── logback │ ├── AbstractWhisperAppender.java │ ├── LogbackMessage.java │ └── WhisperAppender.java │ └── spi │ ├── AbstractMessage.java │ ├── Message.java │ └── MessageWriter.java ├── sample └── resources │ └── whisper-logback-sample.xml └── test ├── java └── com │ └── eclecticlogic │ └── whisper │ └── core │ ├── LogTester.java │ ├── LogTesterRunDigestBeforeSecondMessage.java │ └── ParameterUtilTest.java └── resources ├── testng.xml ├── whisper-logback-fast-digest-test.xml └── whisper-logback-test.xml /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | test-output/ 3 | /target 4 | /.settings 5 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | whisper 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.m2e.core.maven2Nature 21 | org.eclipse.jdt.core.javanature 22 | 23 | 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # What is Whisper? 2 | 3 | Have you ever had something go wrong in your production server and had your Logback email appender immediately send you an email alert? You probably felt like a DevOps guru because you knew exactly what caused the error before anyone else. But then you realize that you've got a database issue and every user request from the app results in an error email (possibly more than one). Right at that time a backend process launches and tries to connect to the database and all hell breaks loose because your email server is now straining under the error email deluge - all 50,000 of them saying the same thing - some variation of "could not connect to database"! 4 | 5 | Perhaps you weren't so fortunate to see this happen in real-time. Perhaps the first time you noticed it was when you got back into work and saw the 50,000 new emails awaiting your attention. And as you sit and delete page after page of email, you have this suspicion that perhaps somewhere in those 50,000 emails (all looking alike) there is a one-off error email that may be important and you are going to accidentally delete it. 6 | 7 | Whisper is your solution to controlling error email spam. Whisper acts as a pass through appender to your default SMTP appender for emails. 8 | When the frequency of a message exceeds the configured threshold, Whisper starts to suppress it. While suppression is on, Whisper will even send you periodic digests letting you know which messages were suppressed and how many of them were suppressed. When things gets resolved and your error message frequency drops, suppression is stopped and things resume as normal. All of this happens on a per log message basis (not including the timestamp and factoring away parameters if you used the parameterized SLF4j log format). 9 | 10 | ### What Logging frameworks does Whisper support? 11 | Whisper currently supports Logback. We hope to add support for log4j and log4jv2 in the future. 12 | 13 | ### How do I get the JAR? 14 | Whisper is available via Maven Central repo. 15 | 16 | ``` 17 | com.eclecticlogic 18 | whisper 19 | jar 20 | 1.0.4 21 | ``` 22 | 23 | ### How do I configure Whisper? 24 | You can refer to the whisper-logback-sample.xml under src/sample/resources. Here is the gist of how to configure Whisper for use with Logback to 25 | handle the most common case of suppressing ERROR messages that are sent via the email appender. 26 | 27 | To configure the Whisper appender, first you must configure two other appenders - the regular email appender for ERROR level logs and a second 28 | email appender for sending the suppression Digests when suppression is actually in effect. 29 | 30 | ``` 31 | 32 | 33 | ERROR 34 | ACCEPT 35 | DENY 36 | 37 | ADDRESS-OF-YOUR-SMTP-HOST 38 | EMAIL-DESTINATION 39 | SENDER-EMAIL 40 | TESTING: %logger{20} - %m 41 | 42 | %date %-5level %logger{35} - %message%n 43 | 44 | 45 | 46 | 47 | ADDRESS-OF-YOUR-SMTP-HOST 48 | EMAIL-DESTINATION 49 | SENDER-EMAIL 50 | %X{whisper.digest.subject} 51 | 52 | %date %-5level %logger{35} - %message%n 53 | 54 | 55 | ``` 56 | 57 | Note the use of `%X{whisper.digest.subject}` for the subject of the digest appender. 58 | 59 | 60 | We now configure the Whisper appender as shown below: 61 | 62 | ``` 63 | 65 | 66 | 67 | ERROR 68 | ACCEPT 69 | DENY 70 | 71 | 73 | digest.appender.logger 74 | 76 | 77 | 3 in 5 minutes 78 | 79 | 4 minutes 80 | 82 | 83 | 20 minutes 84 | 85 | 86 | 87 | 88 | ``` 89 | 90 | The digest logger name is then associated with the digestAppender and the whisper appender is included in the 91 | list of default appenders: 92 | 93 | ``` 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | ``` 103 | 104 | ### What is the license? 105 | Good old [Apache License](http://apache.org/licenses/LICENSE-2.0.html). 106 | 107 | # Release Notes 108 | 109 | ### v1.0.4 110 | 111 | - Fixed [bug](https://github.com/eclecticlogic/whisper/issues/6) caused when the log message itself is null. 112 | 113 | ### v1.0.3 114 | 115 | - Fixed [bug](https://github.com/eclecticlogic/whisper/issues/4) that leaves Muffler in the map 116 | 117 | ### v1.0.2 118 | 119 | - Fixed issue causing digest to continue to report no suppression as described [here](https://github.com/eclecticlogic/whisper/issues/3). 120 | 121 | ### v1.0.1 122 | 123 | - Fixed issue (NPE on digest run) described [here](https://github.com/eclecticlogic/whisper/issues/1). 124 | 125 | 126 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | Whisper - The logback appender with smart suppression. 5 | 6 | com.eclecticlogic 7 | whisper 8 | jar 9 | 1.0.5-SNAPSHOT 10 | 11 | Logback appender that suppresses messages of the same kind when they exceed a defined frequency 12 | and sends out a periodic digest of such suppressed messages. 13 | https://github.com/eclecticlogic/whisper 14 | 15 | 16 | The Apache Software License, Version 2.0 17 | http://www.apache.org/licenses/LICENSE-2.0.txt 18 | Repo 19 | 20 | 21 | 22 | 23 | git@github.com:eclecticlogic/whisper.gt 24 | scm:git:git@github.com:eclecticlogic/whisper.git 25 | scm:git:git@github.com:eclecticlogic/whisper.git 26 | HEAD 27 | 28 | 29 | 30 | 31 | kabram 32 | Karthik Abram 33 | karthik@eclecticlogic.com 34 | 35 | 36 | 37 | 38 | 39 | release 40 | 41 | 42 | performRelease 43 | true 44 | 45 | 46 | 47 | 48 | 49 | 50 | org.apache.maven.plugins 51 | maven-source-plugin 52 | 2.2.1 53 | 54 | 55 | attach-sources 56 | 57 | jar-no-fork 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.apache.maven.plugins 65 | maven-javadoc-plugin 66 | 2.9.1 67 | 68 | 69 | attach-javadocs 70 | 71 | jar 72 | 73 | 74 | 75 | 76 | -Xdoclint:none 77 | 78 | 79 | 80 | 81 | org.apache.maven.plugins 82 | maven-gpg-plugin 83 | 84 | 85 | sign-artifacts 86 | verify 87 | 88 | sign 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | org.apache.maven.plugins 103 | maven-compiler-plugin 104 | 3.1 105 | 106 | 1.6 107 | 1.6 108 | 109 | 110 | 111 | 112 | org.apache.maven.plugins 113 | maven-release-plugin 114 | 2.5 115 | 116 | forked-path 117 | true 118 | false 119 | release 120 | deploy 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | org.apache.maven.plugins 130 | maven-surefire-plugin 131 | 2.17 132 | 133 | 134 | src/test/resources/testng.xml 135 | 136 | 137 | 138 | 139 | 140 | org.sonatype.plugins 141 | nexus-staging-maven-plugin 142 | 1.6 143 | true 144 | 145 | ossrh 146 | https://oss.sonatype.org/ 147 | true 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | ossrh 157 | https://oss.sonatype.org/content/repositories/snapshots 158 | 159 | 160 | 161 | 162 | 163 | ch.qos.logback 164 | logback-classic 165 | 1.2.0 166 | provided 167 | 168 | 169 | 170 | org.testng 171 | testng 172 | 6.8.7 173 | test 174 | 175 | 176 | 177 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/core/Digest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.core; 18 | 19 | import java.text.MessageFormat; 20 | import java.util.ArrayList; 21 | import java.util.List; 22 | 23 | /** 24 | * This class contains all the messages suppressed since the last report (digest). 25 | * 26 | * @author Karthik Abram 27 | */ 28 | public class Digest { 29 | 30 | private List digestMessages = new ArrayList(); 31 | 32 | 33 | public boolean isMessagesSuppressed() { 34 | return digestMessages.size() > 0; 35 | } 36 | 37 | 38 | public String getSubject() { 39 | if (digestMessages.size() == 1) { 40 | DigestMessage dm = digestMessages.get(0); 41 | return MessageFormat.format("[{0}:{1}] {2}", dm.getMessagesSinceLastDigest(), dm.getMessagesSinceStart(), 42 | dm.getMessage()); 43 | } else { 44 | int sum = 0; 45 | for (DigestMessage dm : digestMessages) { 46 | sum += dm.getMessagesSinceLastDigest(); 47 | } 48 | return MessageFormat.format("{0} distinct messages suppressed. Total suppressed since last digest: {1}", 49 | digestMessages.size(), sum); 50 | } 51 | 52 | } 53 | 54 | 55 | public String getMessage() { 56 | StringBuilder builder = new StringBuilder(); 57 | for (DigestMessage dm : digestMessages) { 58 | builder.append(dm.getDetails()); 59 | builder.append("\n-------------------------------------------------------------------\n"); 60 | builder.append("\n\n\n"); 61 | } 62 | return builder.toString(); 63 | } 64 | 65 | 66 | public void add(DigestMessage dm) { 67 | digestMessages.add(dm); 68 | } 69 | 70 | 71 | public void clear() { 72 | digestMessages.clear(); 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/core/DigestMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.core; 18 | 19 | /** 20 | * Each unique log message that was suppressed that will be reported in the periodic digest is modeled as a 21 | * DigestMessage. 22 | * 23 | * @author Karthik Abram 24 | * 25 | */ 26 | public class DigestMessage { 27 | 28 | private int messagesSinceLastDigest; 29 | private int messagesSinceSuppressionStart; 30 | private String fullMessage; 31 | private String message; 32 | 33 | 34 | public String getMessage() { 35 | return message; 36 | } 37 | 38 | 39 | public void setMessage(String message) { 40 | this.message = message; 41 | } 42 | 43 | 44 | public int getMessagesSinceLastDigest() { 45 | return messagesSinceLastDigest; 46 | } 47 | 48 | 49 | public void setMessagesSinceLastDigest(int messagesSinceLastDigest) { 50 | this.messagesSinceLastDigest = messagesSinceLastDigest; 51 | } 52 | 53 | 54 | public int getMessagesSinceStart() { 55 | return messagesSinceSuppressionStart; 56 | } 57 | 58 | 59 | public void setMessagesSinceSuppressionStart(int messagesSinceSuppressionStart) { 60 | this.messagesSinceSuppressionStart = messagesSinceSuppressionStart; 61 | } 62 | 63 | 64 | public String getFullMessage() { 65 | return fullMessage; 66 | } 67 | 68 | 69 | public void setFullMessage(String message) { 70 | this.fullMessage = message; 71 | } 72 | 73 | 74 | public String getDetails() { 75 | StringBuilder builder = new StringBuilder(); 76 | builder.append("Log: ").append(getMessage()).append("\n"); 77 | builder.append("Since last digest: ").append(getMessagesSinceLastDigest()).append("\n"); 78 | builder.append("Since suppression start: ").append(getMessagesSinceStart()).append("\n"); 79 | builder.append("Details: ").append(getFullMessage()).append("\n"); 80 | return builder.toString(); 81 | } 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/core/Muffler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.core; 18 | 19 | import java.util.concurrent.atomic.AtomicInteger; 20 | import java.util.concurrent.atomic.AtomicReference; 21 | 22 | import com.eclecticlogic.whisper.spi.Message; 23 | 24 | /** 25 | * Handles the core logic of figuring out if messages should be suppressed. 26 | * 27 | * @author Karthik Abram 28 | * 29 | */ 30 | public class Muffler { 31 | private final String messageKey; 32 | private boolean inSuppression; 33 | private AtomicInteger messagesSinceSuppressionStart = new AtomicInteger(); 34 | private AtomicInteger messagesSinceLastDigest = new AtomicInteger(); 35 | private AtomicReference> lastMessage = new AtomicReference>(); 36 | 37 | private SuppressionQueue queue = new SuppressionQueue(); 38 | private WhisperManager manager; 39 | 40 | 41 | public Muffler(WhisperManager manager, String messageKey) { 42 | super(); 43 | this.manager = manager; 44 | this.messageKey = messageKey; 45 | queue.setSuppressAfter(manager.getSuppressAfter()); 46 | } 47 | 48 | 49 | /** 50 | * @param message Message to log (and possible suppress). 51 | */ 52 | public void log(Message message) { 53 | if (inSuppression) { 54 | Message msg = lastMessage.get(); 55 | if (msg != null && msg.getMessageAge() > manager.getSuppressionExpirationTime()) { 56 | // Suppression ends 57 | inSuppression = false; 58 | manager.remove(this.messageKey); 59 | // Re-attempt to log this. 60 | manager.log(message); 61 | } else { 62 | messagesSinceSuppressionStart.incrementAndGet(); 63 | messagesSinceLastDigest.incrementAndGet(); 64 | lastMessage.set(message); 65 | } 66 | } else { 67 | queue.offer(message.getMessageTime()); 68 | if (queue.size() > manager.getSuppressionOnMessagesCount()) { 69 | // Start suppression 70 | queue.clear(); 71 | inSuppression = true; 72 | // Circle back in to invoke suppression behavior. 73 | log(message); 74 | } else { 75 | // log 76 | manager.logThrough(message); 77 | } 78 | } 79 | } 80 | 81 | 82 | /** 83 | * Record message suppression digest. 84 | * @param digest 85 | */ 86 | public void digest(Digest digest) { 87 | // Write digest only if we've suppressed something. Without the null check, an error message that is written 88 | // just once will cause a NPE (see https://github.com/eclecticlogic/whisper/issues/1) 89 | Message m = lastMessage.get(); 90 | if (m != null) { 91 | // Added check to see if any messages have been supressed since last digest otherwise 92 | // code sends suppression message indicating nothing happened. 93 | // Refer to https://github.com/eclecticlogic/whisper/issues/3 94 | if (this.messagesSinceLastDigest.get() == 0) { 95 | if (m.getMessageAge() > this.manager.getSuppressionExpirationTime()) { 96 | // Suppression ends 97 | inSuppression = false; 98 | manager.remove(this.messageKey); 99 | lastMessage.set(null); 100 | } else { 101 | // We haven't received any new messages but we are not outside the suppression time either. 102 | // Do nothing. 103 | } 104 | } else { 105 | DigestMessage dm = new DigestMessage(); 106 | dm.setMessage(m.getMessage()); 107 | dm.setFullMessage(m.getFullMessage()); 108 | dm.setMessagesSinceLastDigest(messagesSinceLastDigest.get()); 109 | dm.setMessagesSinceSuppressionStart(messagesSinceSuppressionStart.get()); 110 | messagesSinceLastDigest.set(0); 111 | digest.add(dm); 112 | } 113 | } 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/core/ParameterUtil.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.core; 18 | 19 | import java.util.regex.Matcher; 20 | import java.util.regex.Pattern; 21 | 22 | /** 23 | * Can handle time specified as follows: 24 | * m, min, minute, minutes = Minutes 25 | * s, sec, secs, second, seconds = Seconds 26 | * h, hour, hours, hr, hrs = Hours 27 | * @author Karthik Abram. 28 | * 29 | */ 30 | public class ParameterUtil { 31 | 32 | private static final Pattern numbers = Pattern.compile("^([0-9]+).*"); 33 | private static final Pattern units = Pattern.compile("[0-9\\s]+([a-z]+\\.*)$"); 34 | private static final Pattern timePart = Pattern.compile(".*in([0-9\\s]+[a-z]+\\.*)$"); 35 | 36 | private static long toMillis(String encodedValue) { 37 | String input = encodedValue.trim().toLowerCase(); 38 | Matcher matcherMatcher = numbers.matcher(input); 39 | if (matcherMatcher.matches()) { 40 | String number = matcherMatcher.group(1); 41 | int value = Integer.parseInt(number); 42 | Matcher unitsMatcher = units.matcher(input); 43 | if (unitsMatcher.matches()) { 44 | String unit = unitsMatcher.group(1); 45 | if (unit.startsWith("s")) { 46 | return value * 1000; 47 | } else if (unit.startsWith("m")) { 48 | return value * 60 * 1000; 49 | } else if (unit.startsWith("h")) { 50 | return value * 3600 * 1000; 51 | } 52 | } 53 | } 54 | return -1; 55 | } 56 | 57 | /** 58 | * @param digestFrequency String of the form '5 minutes' or '5 min' etc. 59 | * @return Time in milliseconds 60 | */ 61 | public static long digestFrequencyToMillis(String digestFrequency) { 62 | return toMillis(digestFrequency); 63 | } 64 | 65 | /** 66 | * @param expireAfter Of the form '5 minutes' or '120 seconds' or '3 hrs.' etc. 67 | * @return Time in milliseconds. 68 | */ 69 | public static long expireAfterToMillis(String expireAfter) { 70 | return toMillis(expireAfter); 71 | } 72 | 73 | /** 74 | * @param suppressionExpression A string of the form ' in '. 75 | * e.g. '5 in 3 minutes' 76 | * @return Number of messages expected in timeframe 77 | */ 78 | public static int messageCountForSuppression(String suppressionExpression) { 79 | String[] parts = suppressionExpression.split(" *in *"); 80 | if (parts != null && parts.length > 1) { 81 | return Integer.parseInt(parts[0].trim()); 82 | } 83 | return -1; 84 | } 85 | 86 | /** 87 | * @param suppressionExpression Suppression time expressed in the form '5 in 3 minutes'. 88 | * @return Duration part of the encoded expression in millis 89 | */ 90 | public static long suppressionTimeForSuppression(String suppressionExpression) { 91 | Matcher matcher = timePart.matcher(suppressionExpression.trim()); 92 | if (matcher.matches()) { 93 | return toMillis(matcher.group(1)); 94 | } else { 95 | return -1; 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/core/SuppressionQueue.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.core; 18 | 19 | import java.util.LinkedList; 20 | import java.util.Queue; 21 | 22 | /** 23 | * A queue that keeps all messages that arrive within the suppression trigger period and allows the Muffler to 24 | * determine if suppression should start. 25 | * 26 | * @author kabram 27 | * 28 | */ 29 | @SuppressWarnings("serial") 30 | public class SuppressionQueue extends LinkedList implements Queue { 31 | 32 | private long suppressAfterMillis; 33 | 34 | 35 | /** 36 | * @param suppressAfterMillis Time to suppress after in millis 37 | */ 38 | public void setSuppressAfter(long suppressAfterMillis) { 39 | this.suppressAfterMillis = suppressAfterMillis; 40 | } 41 | 42 | 43 | @Override 44 | public boolean add(Long e) { 45 | // Remove messages that are older than suppression time. 46 | removeExpiredMessages(); 47 | return super.add(e); 48 | } 49 | 50 | 51 | private void removeExpiredMessages() { 52 | long now = System.currentTimeMillis(); 53 | while (size() > 0 && (now - getFirst()) > suppressAfterMillis) { 54 | remove(); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/core/WhisperManager.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.core; 18 | 19 | import java.util.Timer; 20 | import java.util.TimerTask; 21 | import java.util.concurrent.ConcurrentHashMap; 22 | import java.util.concurrent.ConcurrentMap; 23 | 24 | import com.eclecticlogic.whisper.spi.Message; 25 | import com.eclecticlogic.whisper.spi.MessageWriter; 26 | 27 | /** 28 | * The core logic is coordinated by this class. It tracks mufflers for various error messages and schedules digests. 29 | * 30 | * @author Karthik Abram 31 | * 32 | */ 33 | public class WhisperManager extends TimerTask { 34 | 35 | private int suppressionOnMessagesCount; 36 | private long suppressAfterMillis; 37 | 38 | private long suppressionExpirationAfterMillis; 39 | 40 | private MessageWriter writer; 41 | 42 | private ConcurrentMap> queuesByMessage = new ConcurrentHashMap>(); 43 | private Timer digestTimer; 44 | 45 | 46 | public WhisperManager(MessageWriter writer, String supressionAfter, String expireAfter) { 47 | super(); 48 | this.suppressAfterMillis = ParameterUtil.suppressionTimeForSuppression(supressionAfter); 49 | this.suppressionOnMessagesCount = ParameterUtil.messageCountForSuppression(supressionAfter); 50 | this.suppressionExpirationAfterMillis = ParameterUtil.expireAfterToMillis(expireAfter); 51 | 52 | this.writer = writer; 53 | } 54 | 55 | 56 | /** 57 | * @return milliseconds to suppress after. 58 | */ 59 | public long getSuppressAfter() { 60 | return suppressAfterMillis; 61 | } 62 | 63 | 64 | /** 65 | * @return Number of messages to suppress after. 66 | */ 67 | public int getSuppressionOnMessagesCount() { 68 | return suppressionOnMessagesCount; 69 | } 70 | 71 | 72 | /** 73 | * @return Time (in millis) to expire suppression if no messages received in that timeframe. 74 | */ 75 | public long getSuppressionExpirationTime() { 76 | return suppressionExpirationAfterMillis; 77 | } 78 | 79 | 80 | public void log(Message message) { 81 | String messageKey = message.getCanonicalMessage(); 82 | // See https://github.com/eclecticlogic/whisper/issues/6 83 | if (messageKey == null) { 84 | messageKey = "null"; // ConcurrentHashMap will throw a NPE otherwise. 85 | } 86 | Muffler muffler = queuesByMessage.get(messageKey); 87 | if (muffler == null) { 88 | muffler = new Muffler(this, messageKey); 89 | Muffler temp = queuesByMessage.putIfAbsent(messageKey, muffler); 90 | if (temp != null) { 91 | muffler = temp; 92 | } 93 | } 94 | muffler.log(message); 95 | } 96 | 97 | 98 | public void remove(String messageKey) { 99 | queuesByMessage.remove(messageKey); 100 | } 101 | 102 | 103 | /** 104 | * @param message Logs through to the attached appender for immediate logging. 105 | */ 106 | public void logThrough(Message message) { 107 | writer.logThrough(message); 108 | } 109 | 110 | 111 | public void start(long digestFrequencyInMillis) { 112 | digestTimer = new Timer("whisper-timer", true); 113 | digestTimer.scheduleAtFixedRate(this, digestFrequencyInMillis, digestFrequencyInMillis); 114 | } 115 | 116 | 117 | public void stop() { 118 | digestTimer.cancel(); 119 | } 120 | 121 | 122 | @Override 123 | public void run() { 124 | Digest digest = new Digest(); 125 | for (Muffler muffler : queuesByMessage.values()) { 126 | muffler.digest(digest); 127 | } 128 | if (digest.isMessagesSuppressed()) { 129 | writer.logDigest(digest); 130 | digest.clear(); 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/logback/AbstractWhisperAppender.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.logback; 18 | 19 | import java.util.Iterator; 20 | 21 | import ch.qos.logback.classic.spi.ILoggingEvent; 22 | import ch.qos.logback.core.Appender; 23 | import ch.qos.logback.core.AppenderBase; 24 | import ch.qos.logback.core.spi.AppenderAttachable; 25 | import ch.qos.logback.core.spi.AppenderAttachableImpl; 26 | 27 | /** 28 | * @author kabram. 29 | * 30 | */ 31 | public abstract class AbstractWhisperAppender extends AppenderBase implements 32 | AppenderAttachable { 33 | 34 | protected static final String MDC_DIGEST_SUBJECT = "whisper.digest.subject"; 35 | private AppenderAttachableImpl appenderAttachableDelegate = new AppenderAttachableImpl(); 36 | 37 | 38 | @Override 39 | public void addAppender(Appender newAppender) { 40 | appenderAttachableDelegate.addAppender(newAppender); 41 | } 42 | 43 | 44 | @Override 45 | public Iterator> iteratorForAppenders() { 46 | return appenderAttachableDelegate.iteratorForAppenders(); 47 | } 48 | 49 | 50 | @Override 51 | public Appender getAppender(String name) { 52 | return appenderAttachableDelegate.getAppender(name); 53 | } 54 | 55 | 56 | @Override 57 | public boolean isAttached(Appender appender) { 58 | return appenderAttachableDelegate.isAttached(appender); 59 | } 60 | 61 | 62 | @Override 63 | public void detachAndStopAllAppenders() { 64 | appenderAttachableDelegate.detachAndStopAllAppenders(); 65 | } 66 | 67 | 68 | @Override 69 | public boolean detachAppender(Appender appender) { 70 | return appenderAttachableDelegate.detachAppender(appender); 71 | } 72 | 73 | 74 | @Override 75 | public boolean detachAppender(String name) { 76 | return appenderAttachableDelegate.detachAppender(name); 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/logback/LogbackMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.logback; 18 | 19 | import java.util.Date; 20 | 21 | import ch.qos.logback.classic.spi.ILoggingEvent; 22 | import ch.qos.logback.classic.spi.ThrowableProxyUtil; 23 | 24 | import com.eclecticlogic.whisper.spi.AbstractMessage; 25 | 26 | /** 27 | * @author Karthik Abram 28 | * 29 | */ 30 | public class LogbackMessage extends AbstractMessage { 31 | 32 | public LogbackMessage(ILoggingEvent event) { 33 | super(event); 34 | } 35 | 36 | 37 | @Override 38 | public long getMessageAge() { 39 | return new Date().getTime() - getEvent().getTimeStamp(); 40 | } 41 | 42 | 43 | @Override 44 | public long getMessageTime() { 45 | return getEvent().getTimeStamp(); 46 | } 47 | 48 | 49 | @Override 50 | public String getCanonicalMessage() { 51 | return getEvent().getMessage(); 52 | } 53 | 54 | 55 | @Override 56 | public String getMessage() { 57 | return getEvent().getFormattedMessage(); 58 | } 59 | 60 | 61 | @Override 62 | public String getFullMessage() { 63 | if (getEvent().getThrowableProxy() != null) { 64 | return ThrowableProxyUtil.asString(getEvent().getThrowableProxy()); 65 | } else { 66 | return getMessage(); 67 | } 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/logback/WhisperAppender.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.logback; 18 | 19 | import java.util.Iterator; 20 | 21 | import org.slf4j.LoggerFactory; 22 | import org.slf4j.MDC; 23 | 24 | import ch.qos.logback.classic.spi.ILoggingEvent; 25 | import ch.qos.logback.core.Appender; 26 | 27 | import com.eclecticlogic.whisper.core.Digest; 28 | import com.eclecticlogic.whisper.core.ParameterUtil; 29 | import com.eclecticlogic.whisper.core.WhisperManager; 30 | import com.eclecticlogic.whisper.spi.Message; 31 | import com.eclecticlogic.whisper.spi.MessageWriter; 32 | 33 | /** 34 | * @author kabram 35 | * 36 | */ 37 | public class WhisperAppender extends AbstractWhisperAppender implements MessageWriter { 38 | 39 | 40 | private WhisperManager whisperManager; 41 | private String digestLoggerName; 42 | private String suppressAfter; 43 | private String expireAfter; 44 | private String digestFrequency; 45 | 46 | 47 | public String getSuppressAfter() { 48 | return suppressAfter; 49 | } 50 | 51 | 52 | public void setSuppressAfter(String suppressionAfter) { 53 | this.suppressAfter = suppressionAfter; 54 | } 55 | 56 | 57 | public String getExpireAfter() { 58 | return expireAfter; 59 | } 60 | 61 | 62 | public void setExpireAfter(String expireAfter) { 63 | this.expireAfter = expireAfter; 64 | } 65 | 66 | 67 | public String getDigestFrequency() { 68 | return digestFrequency; 69 | } 70 | 71 | 72 | public void setDigestFrequency(String digestFrequency) { 73 | this.digestFrequency = digestFrequency; 74 | } 75 | 76 | 77 | public String getDigestLoggerName() { 78 | return digestLoggerName; 79 | } 80 | 81 | 82 | public void setDigestLoggerName(String digestLoggerName) { 83 | this.digestLoggerName = digestLoggerName; 84 | } 85 | 86 | 87 | @Override 88 | protected void append(ILoggingEvent event) { 89 | LogbackMessage message = new LogbackMessage(event); 90 | whisperManager.log(message); 91 | } 92 | 93 | 94 | @Override 95 | public void logThrough(Message message) { 96 | Iterator> it = iteratorForAppenders(); 97 | while (it.hasNext()) { 98 | it.next().doAppend(message.getEvent()); 99 | } 100 | } 101 | 102 | 103 | @Override 104 | public void logDigest(Digest digest) { 105 | MDC.put(MDC_DIGEST_SUBJECT, digest.getSubject()); 106 | LoggerFactory.getLogger(getDigestLoggerName()).error(digest.getMessage()); 107 | } 108 | 109 | 110 | @Override 111 | public void start() { 112 | super.start(); 113 | whisperManager = new WhisperManager(this, getSuppressAfter(), getExpireAfter()); 114 | whisperManager.start(ParameterUtil.digestFrequencyToMillis(getDigestFrequency())); 115 | } 116 | 117 | 118 | @Override 119 | public void stop() { 120 | whisperManager.stop(); 121 | super.stop(); 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/spi/AbstractMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.spi; 18 | 19 | 20 | /** 21 | * @author Karthik Abram 22 | * 23 | */ 24 | public abstract class AbstractMessage implements Message { 25 | 26 | private E event; 27 | 28 | public AbstractMessage(E event) { 29 | super(); 30 | this.event = event; 31 | } 32 | 33 | @Override 34 | public E getEvent() { 35 | return event; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/spi/Message.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.spi; 18 | 19 | /** 20 | * Every logging implementation's message needs to expose this interface. This is done by wrapping the message in a 21 | * wrapper that implements this interface (and uses AbstractMessage as a convenient base class). 22 | * 23 | * @author Karthik Abram 24 | * 25 | */ 26 | public interface Message { 27 | 28 | /** 29 | * @return How long ago (in milliseconds) this message was logged. 30 | */ 31 | long getMessageAge(); 32 | 33 | /** 34 | * @return Timestamp of this message in milliseconds since epoch. 35 | */ 36 | long getMessageTime(); 37 | 38 | /** 39 | * @return Message string without parameters (if the underlying logging implementation allows for this). 40 | */ 41 | String getCanonicalMessage(); 42 | 43 | /** 44 | * @return Underlying implementation specific message class. 45 | */ 46 | E getEvent(); 47 | 48 | /** 49 | * @return Formatted message (for exceptions, this does not include stack trace). 50 | */ 51 | String getMessage(); 52 | 53 | /** 54 | * @return The fully formatted message (for exceptions, this includes call stack). 55 | */ 56 | String getFullMessage(); 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/com/eclecticlogic/whisper/spi/MessageWriter.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | package com.eclecticlogic.whisper.spi; 18 | 19 | import com.eclecticlogic.whisper.core.Digest; 20 | 21 | 22 | /** 23 | * Every logging appender should expose this interface to log messages that are not suppressed and to send log digests. 24 | * 25 | * @author Karthik Abram 26 | * 27 | */ 28 | public interface MessageWriter { 29 | 30 | /** 31 | * Writes the message through to the attached appender for immediate logging. 32 | * @param message Message to log. 33 | */ 34 | void logThrough(Message message); 35 | 36 | /** 37 | * Writes the digest message through to the attached appender. 38 | * @param digest 39 | */ 40 | void logDigest(Digest digest); 41 | } 42 | -------------------------------------------------------------------------------- /src/sample/resources/whisper-logback-sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | testFile.log 6 | true 7 | 8 | %-4relative [%thread] %-5level %logger{35} - %msg%n 9 | 10 | 11 | 12 | 13 | 14 | ADDRESS-OF-YOUR-SMTP-HOST 15 | EMAIL-DESTINATION 16 | ANOTHER_EMAIL_DESTINATION 17 | SENDER-EMAIL 18 | TESTING: %logger{20} - %m 19 | 20 | %date %-5level %logger{35} - %message%n 21 | 22 | 23 | 24 | 25 | 26 | 28 | 29 | 30 | ERROR 31 | ACCEPT 32 | DENY 33 | 34 | 36 | digest.appender.logger 37 | 39 | 3 in 5 minutes 40 | 42 | 4 minutes 43 | 45 | 20 minutes 46 | 47 | 48 | 49 | 50 | 51 | 52 | ADDRESS-OF-YOUR-SMTP-HOST 53 | EMAIL-DESTINATION 54 | ANOTHER_EMAIL_DESTINATION 55 | SENDER-EMAIL 56 | %X{whisper.digest.subject} 57 | 58 | %date %-5level %logger{35} - %message%n 59 | 60 | 61 | 62 | 63 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /src/test/java/com/eclecticlogic/whisper/core/LogTester.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011-2013 Eclectic Logic LLC. 3 | * All rights reserved. 4 | * 5 | * This software is the confidential and proprietary information of 6 | * Eclectic Logic LLC ("Confidential Information"). You shall 7 | * not disclose such Confidential Information and shall use it only 8 | * in accordance with the terms of the license agreement you entered 9 | * into with Eclectic Logic LLC. 10 | * 11 | **/ 12 | package com.eclecticlogic.whisper.core; 13 | 14 | import java.util.concurrent.Semaphore; 15 | 16 | import org.slf4j.Logger; 17 | import org.slf4j.LoggerFactory; 18 | 19 | /** 20 | * @author kabram. 21 | * 22 | */ 23 | public class LogTester implements Runnable { 24 | 25 | private static Logger logger = LoggerFactory.getLogger(LogTester.class); 26 | 27 | private static Semaphore canQuit = new Semaphore(0); 28 | 29 | 30 | @Override 31 | public void run() { 32 | for (int i = 0; i < 45; i++) { 33 | logger.error("Blah error occured for the {} time", i); 34 | logger.error(null); // Ensure this doesn't break anything. 35 | try { 36 | Thread.sleep(1000); 37 | } catch (InterruptedException e) { 38 | } 39 | } 40 | try { 41 | Thread.sleep(10000); 42 | } catch (InterruptedException e) { 43 | } 44 | 45 | logger.error("This should show up in both logs."); 46 | canQuit.release(); 47 | } 48 | 49 | 50 | /** 51 | * @param args 52 | */ 53 | public static void main(String[] args) throws InterruptedException { 54 | Thread t = new Thread(new LogTester()); 55 | t.setDaemon(true); 56 | t.start(); 57 | 58 | canQuit.acquire(); 59 | 60 | // Digests should be written twice and then stop all-together. The last message should appear in the console. 61 | try { 62 | Thread.sleep(5 * 1000); 63 | } catch (InterruptedException e) { 64 | } 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/com/eclecticlogic/whisper/core/LogTesterRunDigestBeforeSecondMessage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2011-2013 Eclectic Logic LLC. 3 | * All rights reserved. 4 | * 5 | * This software is the confidential and proprietary information of 6 | * Eclectic Logic LLC ("Confidential Information"). You shall 7 | * not disclose such Confidential Information and shall use it only 8 | * in accordance with the terms of the license agreement you entered 9 | * into with Eclectic Logic LLC. 10 | * 11 | **/ 12 | package com.eclecticlogic.whisper.core; 13 | 14 | import org.slf4j.Logger; 15 | import org.slf4j.LoggerFactory; 16 | 17 | /** 18 | * @author kabram. 19 | * 20 | */ 21 | public class LogTesterRunDigestBeforeSecondMessage implements Runnable { 22 | 23 | private static Logger logger = LoggerFactory.getLogger(LogTesterRunDigestBeforeSecondMessage.class); 24 | 25 | 26 | @Override 27 | public void run() { 28 | for (int i = 0; i < 600; i++) { 29 | logger.error("Blah error occured for the {} time", i); 30 | try { 31 | Thread.sleep(15000); 32 | } catch (InterruptedException e) { 33 | } 34 | } 35 | } 36 | 37 | 38 | /** 39 | * @param args 40 | */ 41 | public static void main(String[] args) { 42 | Thread t = new Thread(new LogTesterRunDigestBeforeSecondMessage()); 43 | t.setDaemon(true); 44 | t.start(); 45 | 46 | try { 47 | Thread.sleep(10 * 60 * 1000); 48 | } catch (InterruptedException e) { 49 | } 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/com/eclecticlogic/whisper/core/ParameterUtilTest.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2013 Eclectic Logic LLC. 3 | * All rights reserved. 4 | * 5 | * This software is the confidential and proprietary information of 6 | * Eclectic Logic LLC ("Confidential Information"). You shall 7 | * not disclose such Confidential Information and shall use it only 8 | * in accordance with the terms of the license agreement you entered 9 | * into with Eclectic Logic LLC. 10 | * 11 | **/ 12 | package com.eclecticlogic.whisper.core; 13 | 14 | import static org.testng.Assert.*; 15 | 16 | import org.testng.annotations.Test; 17 | 18 | import com.eclecticlogic.whisper.core.ParameterUtil; 19 | 20 | 21 | /** 22 | * @author Karthik Abram 23 | * 24 | */ 25 | @Test 26 | public class ParameterUtilTest { 27 | 28 | @Test 29 | public void testDigestFrequency() { 30 | assertEquals(ParameterUtil.digestFrequencyToMillis("5s"), 5 * 1000); 31 | assertEquals(ParameterUtil.digestFrequencyToMillis("15s"), 15 * 1000); 32 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 s"), 115 * 1000); 33 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 sec."), 115 * 1000); 34 | assertEquals(ParameterUtil.digestFrequencyToMillis("5 secs."), 5 * 1000); 35 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 second."), 115 * 1000); 36 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 second"), 115 * 1000); 37 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 seconds"), 115 * 1000); 38 | assertEquals(ParameterUtil.digestFrequencyToMillis("115seconds"), 115 * 1000); 39 | 40 | assertEquals(ParameterUtil.digestFrequencyToMillis("5m"), 5 * 60000); 41 | assertEquals(ParameterUtil.digestFrequencyToMillis("15m"), 15 * 60000); 42 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 m"), 115 * 60000); 43 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 min."), 115 * 60000); 44 | assertEquals(ParameterUtil.digestFrequencyToMillis("5 mins."), 5 * 60000); 45 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 minute."), 115 * 60000); 46 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 minute"), 115 * 60000); 47 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 minutes"), 115 * 60000); 48 | assertEquals(ParameterUtil.digestFrequencyToMillis("115minutes"), 115 * 60000); 49 | 50 | assertEquals(ParameterUtil.digestFrequencyToMillis("5h"), 5 * 3600000); 51 | assertEquals(ParameterUtil.digestFrequencyToMillis("15h"), 15 * 3600000); 52 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 h"), 115 * 3600000); 53 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 hr."), 115 * 3600000); 54 | assertEquals(ParameterUtil.digestFrequencyToMillis("5 hrs."), 5 * 3600000); 55 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 hour."), 115 * 3600000); 56 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 hour"), 115 * 3600000); 57 | assertEquals(ParameterUtil.digestFrequencyToMillis("115 hours"), 115 * 3600000); 58 | assertEquals(ParameterUtil.digestFrequencyToMillis("115hours"), 115 * 3600000); 59 | } 60 | 61 | @Test 62 | public void testExpireAfter() { 63 | assertEquals(ParameterUtil.expireAfterToMillis("5s"), 5 * 1000); 64 | assertEquals(ParameterUtil.expireAfterToMillis("15s"), 15 * 1000); 65 | assertEquals(ParameterUtil.expireAfterToMillis("115 s"), 115 * 1000); 66 | assertEquals(ParameterUtil.expireAfterToMillis("115 sec."), 115 * 1000); 67 | assertEquals(ParameterUtil.expireAfterToMillis("5 secs."), 5 * 1000); 68 | assertEquals(ParameterUtil.expireAfterToMillis("115 second."), 115 * 1000); 69 | assertEquals(ParameterUtil.expireAfterToMillis("115 second"), 115 * 1000); 70 | assertEquals(ParameterUtil.expireAfterToMillis("115 seconds"), 115 * 1000); 71 | assertEquals(ParameterUtil.expireAfterToMillis("115seconds"), 115 * 1000); 72 | 73 | assertEquals(ParameterUtil.expireAfterToMillis("5m"), 5 * 60000); 74 | assertEquals(ParameterUtil.expireAfterToMillis("15m"), 15 * 60000); 75 | assertEquals(ParameterUtil.expireAfterToMillis("115 m"), 115 * 60000); 76 | assertEquals(ParameterUtil.expireAfterToMillis("115 min."), 115 * 60000); 77 | assertEquals(ParameterUtil.expireAfterToMillis("5 mins."), 5 * 60000); 78 | assertEquals(ParameterUtil.expireAfterToMillis("115 minute."), 115 * 60000); 79 | assertEquals(ParameterUtil.expireAfterToMillis("115 minute"), 115 * 60000); 80 | assertEquals(ParameterUtil.expireAfterToMillis("115 minutes"), 115 * 60000); 81 | assertEquals(ParameterUtil.expireAfterToMillis("115minutes"), 115 * 60000); 82 | 83 | assertEquals(ParameterUtil.expireAfterToMillis("5h"), 5 * 3600000); 84 | assertEquals(ParameterUtil.expireAfterToMillis("15h"), 15 * 3600000); 85 | assertEquals(ParameterUtil.expireAfterToMillis("115 h"), 115 * 3600000); 86 | assertEquals(ParameterUtil.expireAfterToMillis("115 hr."), 115 * 3600000); 87 | assertEquals(ParameterUtil.expireAfterToMillis("5 hrs."), 5 * 3600000); 88 | assertEquals(ParameterUtil.expireAfterToMillis("115 hour."), 115 * 3600000); 89 | assertEquals(ParameterUtil.expireAfterToMillis("115 hour"), 115 * 3600000); 90 | assertEquals(ParameterUtil.expireAfterToMillis("115 hours"), 115 * 3600000); 91 | assertEquals(ParameterUtil.expireAfterToMillis("115hours"), 115 * 3600000); 92 | } 93 | 94 | @Test 95 | public void testMessageCountForSupression() { 96 | assertEquals(ParameterUtil.messageCountForSuppression("5 in 3 minutes"), 5); 97 | assertEquals(ParameterUtil.messageCountForSuppression("100 in 3 minutes"), 100); 98 | assertEquals(ParameterUtil.messageCountForSuppression("100 "), -1); 99 | assertEquals(ParameterUtil.messageCountForSuppression("100in3 minutes"), 100); 100 | } 101 | 102 | @Test 103 | public void testSuppressionTimeForSuppression() { 104 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 5s"), 5 * 1000); 105 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 15s"), 15 * 1000); 106 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in115 s"), 115 * 1000); 107 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3in115 sec."), 115 * 1000); 108 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 5 secs."), 5 * 1000); 109 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3in115 second."), 115 * 1000); 110 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 second"), 115 * 1000); 111 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 seconds"), 115 * 1000); 112 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115seconds"), 115 * 1000); 113 | 114 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 5m"), 5 * 60000); 115 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 15m"), 15 * 60000); 116 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 m"), 115 * 60000); 117 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in115 min."), 115 * 60000); 118 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3in5mins."), 5 * 60000); 119 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 minute."), 115 * 60000); 120 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 minute"), 115 * 60000); 121 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 minutes"), 115 * 60000); 122 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115minutes"), 115 * 60000); 123 | 124 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 5h"), 5 * 3600000); 125 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 15h"), 15 * 3600000); 126 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 h"), 115 * 3600000); 127 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 hr."), 115 * 3600000); 128 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 5 hrs."), 5 * 3600000); 129 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 hour."), 115 * 3600000); 130 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 hour"), 115 * 3600000); 131 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115 hours"), 115 * 3600000); 132 | assertEquals(ParameterUtil.suppressionTimeForSuppression("3 in 115hours"), 115 * 3600000); 133 | } 134 | } 135 | 136 | -------------------------------------------------------------------------------- /src/test/resources/testng.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/test/resources/whisper-logback-fast-digest-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 8 | 9 | %d{HH:mm:ss.SSS} this is the file logger%n 10 | 11 | 12 | 13 | 14 | 15 | 16 | 18 | 19 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - error-email - %msg%n 20 | 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | ERROR 30 | ACCEPT 31 | DENY 32 | 33 | 35 | digest.appender.logger 36 | 38 | 3 in 5 minutes 39 | 41 | 4 minutes 42 | 44 | 10 seconds 45 | 46 | 47 | 48 | 49 | 50 | 51 | 53 | 54 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %X{whisper.digest.subject} - %msg%n 55 | 56 | 57 | 58 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /src/test/resources/whisper-logback-test.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 8 | 9 | %d{HH:mm:ss.SSS} this is the file logger%n 10 | 11 | 12 | 13 | 14 | 15 | 16 | 18 | 19 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - error-email - %msg%n 20 | 21 | 22 | 23 | 24 | 25 | 27 | 28 | 29 | ERROR 30 | ACCEPT 31 | DENY 32 | 33 | 35 | digest.appender.logger 36 | 38 | 3 in 5 minutes 39 | 41 | 4 minutes 42 | 44 | 30 seconds 45 | 46 | 47 | 48 | 49 | 50 | 51 | 53 | 54 | %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %X{whisper.digest.subject} - %msg%n 55 | 56 | 57 | 58 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | --------------------------------------------------------------------------------