How to get java log for using modified java.util.logging.Level class

In the settings of my Java application, they can set the debugging level, presented as a JComboBox, containing seven default levels. The list contains SEVERE, WARNING, INFO ecetera, which is rather ugly, and Id rather replaces "Severe, Warning" for English.

java.util.logging.Level, which is used in java logging, has constructors that can use a different set of resources, so I was thinking of a subclass Level so that I could pass the resource package myself.

But how do I get a java log to use changed levels

EDIT

A modified version of the example class is used, given in the answers 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 you need to add the 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()))); 
+4
source share
2 answers

You should not change classes inside the java package. This is part of the JDK. And obviously this is not an option for a graphical interface. Just learn how to use JComboBox to visualize values ​​in different ways. For instance. you can 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 perhaps you can create your own renderer. For example, how to create a custom rednerer: How to use the Map element as JComboBox text

+4
source

Each registration level in the Level has a corresponding int. If you want to filter what levels of events you want to log, you can try something long in the lines,

 if (event.getLevel().intValue() > Level.INFO.intValue()) { event.log(); } 

Then only WAR WARNING and SEVERE will be recorded.

If you want to change the values ​​of these levels, I think you can create your own MyLevels class, which contains static instances of the Level with the values ​​you want.

+1
source

All Articles