forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for logging into console for common logs (alibaba#836)
* Add a ConsoleHandler to support logging into stdout and stderr. * Add a `csp.sentinel.log.output.type` property to configure for output type of record logs (only a temporary design) * Add millisecond to the format of CspFormatter
- Loading branch information
Showing
4 changed files
with
216 additions
and
13 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/ConsoleHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.log; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
import java.util.logging.*; | ||
|
||
/** | ||
* This Handler publishes log records to console by using {@link java.util.logging.StreamHandler}. | ||
* | ||
* Print log of WARNING level or above to System.err, | ||
* and print log of INFO level or below to System.out. | ||
* | ||
* To use this handler, add the following VM argument: | ||
* <pre> | ||
* -Dcsp.sentinel.log.output.type=console | ||
* </pre> | ||
* | ||
* @author cdfive | ||
*/ | ||
class ConsoleHandler extends Handler { | ||
|
||
/** | ||
* A Handler which publishes log records to System.out. | ||
*/ | ||
private StreamHandler stdoutHandler; | ||
|
||
/** | ||
* A Handler which publishes log records to System.err. | ||
*/ | ||
private StreamHandler stderrHandler; | ||
|
||
public ConsoleHandler() { | ||
this.stdoutHandler = new StreamHandler(System.out, new CspFormatter()); | ||
this.stderrHandler = new StreamHandler(System.err, new CspFormatter()); | ||
} | ||
|
||
@Override | ||
public synchronized void setFormatter(Formatter newFormatter) throws SecurityException { | ||
this.stdoutHandler.setFormatter(newFormatter); | ||
this.stderrHandler.setFormatter(newFormatter); | ||
} | ||
|
||
@Override | ||
public synchronized void setEncoding(String encoding) throws SecurityException, UnsupportedEncodingException { | ||
this.stdoutHandler.setEncoding(encoding); | ||
this.stderrHandler.setEncoding(encoding); | ||
} | ||
|
||
@Override | ||
public void publish(LogRecord record) { | ||
if (record.getLevel().intValue() >= Level.WARNING.intValue()) { | ||
stderrHandler.publish(record); | ||
stderrHandler.flush(); | ||
} else { | ||
stdoutHandler.publish(record); | ||
stdoutHandler.flush(); | ||
} | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
stdoutHandler.flush(); | ||
stderrHandler.flush(); | ||
} | ||
|
||
@Override | ||
public void close() throws SecurityException { | ||
stdoutHandler.close(); | ||
stderrHandler.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
sentinel-core/src/test/java/com/alibaba/csp/sentinel/log/ConsoleHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.alibaba.csp.sentinel.log; | ||
|
||
import org.junit.Test; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.PrintStream; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogRecord; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Test cases for {@link ConsoleHandler}. | ||
* | ||
* @author cdfive | ||
*/ | ||
public class ConsoleHandlerTest { | ||
|
||
@Test | ||
public void testPublish() { | ||
ByteArrayOutputStream baosOut = new ByteArrayOutputStream(); | ||
System.setOut(new PrintStream(baosOut)); | ||
|
||
ByteArrayOutputStream baosErr = new ByteArrayOutputStream(); | ||
System.setErr(new PrintStream(baosErr)); | ||
|
||
CspFormatter cspFormatter = new CspFormatter(); | ||
ConsoleHandler consoleHandler = new ConsoleHandler(); | ||
|
||
LogRecord logRecord; | ||
|
||
// Test INFO level, should log to stdout | ||
logRecord = new LogRecord(Level.INFO, "test info message"); | ||
consoleHandler.publish(logRecord); | ||
assertEquals(cspFormatter.format(logRecord), baosOut.toString()); | ||
assertEquals("", baosErr.toString()); | ||
baosOut.reset(); | ||
baosErr.reset(); | ||
|
||
// Test INFO level, should log to stderr | ||
logRecord = new LogRecord(Level.WARNING, "test warning message"); | ||
consoleHandler.publish(logRecord); | ||
assertEquals(cspFormatter.format(logRecord), baosErr.toString()); | ||
assertEquals("", baosOut.toString()); | ||
baosOut.reset(); | ||
baosErr.reset(); | ||
|
||
// Test FINE level, no log by default | ||
// Default log level is INFO, to log FINE message to stdout, add following config in $JAVA_HOME/jre/lib/logging.properties | ||
// java.util.logging.StreamHandler.level=FINE | ||
logRecord = new LogRecord(Level.FINE, "test fine message"); | ||
consoleHandler.publish(logRecord); | ||
assertEquals("", baosOut.toString()); | ||
assertEquals("", baosErr.toString()); | ||
baosOut.reset(); | ||
baosErr.reset(); | ||
|
||
// Test SEVERE level, should log to stderr | ||
logRecord = new LogRecord(Level.SEVERE, "test severe message"); | ||
consoleHandler.publish(logRecord); | ||
assertEquals(cspFormatter.format(logRecord), baosErr.toString()); | ||
assertEquals("", baosOut.toString()); | ||
baosOut.reset(); | ||
baosErr.reset(); | ||
} | ||
} |