-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eec1673
commit e800afc
Showing
5 changed files
with
133 additions
and
61 deletions.
There are no files selected for viewing
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
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
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,42 @@ | ||
package mockery; | ||
|
||
import java.util.logging.ConsoleHandler; | ||
import java.util.logging.Level; | ||
import java.util.logging.LogRecord; | ||
import java.util.logging.Logger; | ||
import java.util.logging.SimpleFormatter; | ||
import java.util.logging.StreamHandler; | ||
|
||
public class ConsoleLogger extends StreamHandler { | ||
private static String formatLog(LogRecord lr) { | ||
return String.format("[%1$-7s] %2$s%n", lr.getLevel().getName(), lr.getMessage()); | ||
} | ||
|
||
public static Logger createLogger(String name, Level minimumLogLevel) { | ||
Logger logger = Logger.getLogger(name); | ||
logger.setUseParentHandlers(false); | ||
logger.setLevel(minimumLogLevel); | ||
|
||
ConsoleHandler warning2stderrLogger = new ConsoleHandler(); | ||
warning2stderrLogger.setLevel(Level.WARNING); | ||
warning2stderrLogger.setFormatter(new SimpleFormatter() { | ||
@Override | ||
public synchronized String format(LogRecord lr) { | ||
return formatLog(lr); | ||
} | ||
}); | ||
logger.addHandler(warning2stderrLogger); | ||
|
||
ConsoleLogger fine2stdoutLogger = new ConsoleLogger(); | ||
logger.addHandler(fine2stdoutLogger); | ||
|
||
return logger; | ||
} | ||
|
||
@Override | ||
public void publish(LogRecord record) { | ||
final Level level = record.getLevel(); | ||
if (level.intValue() <= Level.INFO.intValue()) | ||
System.out.print(formatLog(record)); | ||
} | ||
} |