-
Notifications
You must be signed in to change notification settings - Fork 12
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
Showing
1 changed file
with
72 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package mb.nabl2.log; | ||
|
||
import org.metaborg.util.log.ILogger; | ||
import org.metaborg.util.log.LoggerUtils; | ||
|
||
public class Logger { | ||
|
||
private static final ILogger log = LoggerUtils.logger(Logger.class); | ||
|
||
private final String name; | ||
|
||
public Logger(String name) { | ||
this.name = truncate(name); | ||
} | ||
|
||
public void debug(String message, Object... args) { | ||
log(format(message, args), "[DEBUG] -"); | ||
} | ||
|
||
public void debug(String message, Throwable ex, Object... args) { | ||
log(format(message, args), "[DEBUG] -"); | ||
ex.printStackTrace(System.out); | ||
} | ||
|
||
public void info(String message, Object... args) { | ||
log(format(message, args), "[INFO] -"); | ||
} | ||
|
||
public void info(String message, Throwable ex, Object... args) { | ||
log(format(message, args), "[INFO] -"); | ||
ex.printStackTrace(System.out); | ||
} | ||
|
||
public void warn(String message, Object... args) { | ||
log(format(message, args), "[WARN] -"); | ||
} | ||
|
||
public void warn(String message, Throwable ex, Object... args) { | ||
log(format(message, args), "[WARN] -"); | ||
ex.printStackTrace(System.out); | ||
} | ||
|
||
public void error(String message, Object... args) { | ||
log(format(message, args), "[ERROR] -"); | ||
} | ||
|
||
public void error(String message, Throwable ex, Object... args) { | ||
log(format(message, args), "[ERROR] -"); | ||
ex.printStackTrace(System.out); | ||
} | ||
|
||
private String format(String format, Object... args) { | ||
return log.format(format, args); | ||
} | ||
|
||
private void log(String message, String level) { | ||
System.out.printf("%s %-32s | %s%n", level, name, message); | ||
} | ||
|
||
private String truncate(String name) { | ||
if(name.length() > 32) { | ||
return name.substring(name.length() - 32); | ||
} else { | ||
return name; | ||
} | ||
} | ||
|
||
public static Logger logger(Class<?> clz) { | ||
return new Logger(clz.getCanonicalName()); | ||
} | ||
|
||
} |