Skip to content

Instantly share code, notes, and snippets.

@vprusa
Created June 29, 2020 14:47
Show Gist options
  • Save vprusa/70866e4df35b19107dafdd788311a93d to your computer and use it in GitHub Desktop.
Save vprusa/70866e4df35b19107dafdd788311a93d to your computer and use it in GitHub Desktop.
Custom multidimensional logger sketch
/**
* Custom multidimensional logger sketch (WildFly compatible?)
*
* TODO checkout
* - https://github.com/jamezp/wildfly-examples/tree/master/custom-log-filter
*/
public final class Log {
// TODO load from env variable, config file, option -D
public static Class[] enabledLogTypes = {
LoggerType1.class,
// LoggerType2.class,
LoggerType3.class
};
public static class LoggerType {}
public static class LoggerType1 extends LoggerType {}
public static class LoggerType2 extends LoggerType {}
public static class LoggerType3 extends LoggerType {}
public static final Logger logger = Logger.getLogger(Log.class.getSimpleName());
public static void info(Object s) {
logger.info(s);
}
public static void info(Class c, Object s) {
logger.info(c.getSimpleName() + ": " + s.toString());
}
public static void infoCreate(Class c, Object s) {
info(c, " created: " + s.toString());
}
public static void infoRemove(Class c, Object s) {
info(c, " removed: " + s.toString());
}
public static void infoUpdate(Class c, Object s) {
info(c, " updated: " + s.toString());
}
public static void infoDelete(Class c, Object s) {
info(c, " deleted: " + s.toString());
}
public static class TypedLogger<T extends LoggerType> {
public final Class t;
public static final Logger logger = Logger.getLogger(Log.class.getSimpleName());
public TypedLogger(Class t) {
this.t = t;
}
public void info(Object o) {
if (Arrays.asList(Log.enabledLogTypes).contains(t)){
Log.info(o);
}
}
}
}
@vprusa
Copy link
Author

vprusa commented Jun 29, 2020

Usage:

public static final Log.TypedLogger log1 = new Log.TypedLogger<Log.LoggerType1>(Log.LoggerType1.class);
// ... 
public static final Log.TypedLogger log2 = new Log.TypedLogger<Log.LoggerType2>(Log.LoggerType2.class);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment