Reputation: 13210
In the preferences of my Java application they can set the debug level presented as a JComboBox containing the seven default levels. The list contains SEVERE,WARNING,INFO ecetera which is rather ugly and Id rather replace with 'Severe, Warning' for english.
java.util.logging.Level as used by java logging has contructors that can take a different resource bundle so I thought of sublassing Level so that i can chnage the resource bundle to my own.
But how do I get java logging to use modified levels
EDIT
Used a modified version of the example class given in kans answer below
class LevelItem extends Object
{
public final Level level;
public LevelItem(Level level)
{
this.level = level;
}
public String toString()
{
return level.getLocalizedName().substring(0,1)+
level.getLocalizedName().substring(1).toLowerCase();
}
public boolean equals(Object obj)
{
return (obj instanceof LevelItem &&
(this.level.equals((((LevelItem) obj).level))));
}
}
but need to add equals method so setSelectedItem() works for me in combobox
i.e
debugLevelCombo = new JComboBox();
debugLevelCombo.addItem(new LevelItem(Level.SEVERE));
debugLevelCombo.addItem(new LevelItem(Level.WARNING));
debugLevelCombo.addItem(new LevelItem(Level.INFO));
debugLevelCombo.addItem(new LevelItem(Level.CONFIG));
debugLevelCombo.addItem(new LevelItem(Level.FINE));
debugLevelCombo.addItem(new LevelItem(Level.FINER));
debugLevelCombo.addItem(new LevelItem(Level.FINEST));
debugLevelCombo.setSelectedItem(new
LevelItem(Level.parse(UserPreferences.getInstance().getDebugLevel())));
Upvotes: 4
Views: 127
Reputation: 28951
You must not change classes inside "java" package. It is part of JDK. And obviously it is not an option for GUI. Just learn more how to use JComboBox to render values differently. E.g. you could create a wrapper class:
class LevelItem
{
private final Level level;
public LevelItem(Level level) {this.level=level;}
public String toString()
{
if(Level.WARNING.equals(level) return "Warning";
if(Level.INFO.equals(level) return "Information";
...
}
}
}
And use it for combobox values. Or maybe it is possible to make a custom renderer. This is example how to create a customized rednerer: How to use Map element as text of a JComboBox
Upvotes: 4
Reputation: 2475
Each of the logging levels in Level has a corresponing int. If you want to filter which levels of event you want to log you can try something a long the lines of,
if (event.getLevel().intValue() > Level.INFO.intValue()) {
event.log();
}
This will then only log WARNING and SEVERE.
If you want to change the values of these levels, I guess you can create you own class MyLevels that contains static instances of Level with the values you want.
Upvotes: 1