Skip to content

Instantly share code, notes, and snippets.

@jderda jderda/ Secret
Last active Oct 15, 2016

What would you like to do?
Tygodniowe wyzwanie programistyczne - #1
package pl.kobietydokodu.wyzwanie;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.PriorityBlockingQueue;
public class Logger {
private static DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("yyyy-MM-dd HH:mm:ssZZ")
private static PriorityBlockingQueue<LogItem> itemsQueue = new PriorityBlockingQueue<>();
private static ExecutorService executor = Executors.newSingleThreadExecutor();
static {
executor.submit(new Runnable() {
public void run() {
try {
while (true) {
LogItem nextItem = itemsQueue.take();
} catch (InterruptedException e) {
public static Logger getLogger(String name) {
return new Logger(name);
public enum LogLevel {
public Logger(String name) {
this.loggerName = name;
private String loggerName;
public void info(String message, Object... parameters) {
writeLog(LogLevel.INFO, message, parameters);
public void warn(String message, Object... parameters) {
writeLog(LogLevel.WARN, message, parameters);
public void error(String message, Object... parameters) {
writeLog(LogLevel.ERROR, message, parameters);
private void writeLog(LogLevel level, String message, Object... parameters) {
String interpolatedMessage = String.format(message, parameters);
LogItem item = new LogItem(loggerName, level, interpolatedMessage);
private class LogItem implements Comparable<LogItem> {
final Instant timestamp;
final String loggerName;
final LogLevel level;
final String interpolatedMessage;
public LogItem(String loggerName, LogLevel level, String interpolatedMessage) {
this.timestamp =;
this.loggerName = loggerName;
this.level = level;
this.interpolatedMessage = interpolatedMessage;
public String toString() {
String timeString = formatter.format(this.timestamp);
return String.format("%s %s [%s]: %s", timeString, this.loggerName,, this.interpolatedMessage);
public int compareTo(LogItem o) {
return this.timestamp.compareTo(o.timestamp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.