当前位置: 首页 > 工具软件 > MicroLog > 使用案例 >

J2ME中使用microlog

李振国
2023-12-01

 

import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;

import net.sf.microlog.core.Logger;
import net.sf.microlog.core.LoggerFactory;
import net.sf.microlog.core.PropertyConfigurator;
public class Hello3D extends MIDlet implements CommandListener
{
  private Command exitCommand = new Command("Exit", Command.EXIT, 1);
  private Logger logger=LoggerFactory.getLogger(Hello3D.class);
  public void startApp()
  {
    PropertyConfigurator.configure("/microlog.properties"); //loads the configuration file
    Form mForm = new Form("Hello 3D Properties:");
    String s = new String();
    Font.getFont(0, 1, 8);
    mForm.append("\nM3G Version: ");
    mForm.append(s + System.getProperty("microedition.m3g.version"));//获取M3G版本属性
    mForm.addCommand(exitCommand);				//添加退出按钮
    mForm.setCommandListener(this);					//设置软键监听
    Display.getDisplay(this).setCurrent(mForm);			//显示Form
    
    logger.info("start");
  }
  public void pauseApp()  {  }
  public void destroyApp(boolean unconditional)  {  }
  public void commandAction(Command command, Displayable displayable)
  {
    if (command == exitCommand)					//当按下Exit软键则退出程序
    {
      destroyApp(true);
      notifyDestroyed();
    }
  }
}

microlog.properties

# Sets the level to log. See log4j documentation for a more in-depth discussion
microlog.level=DEBUG
#
# Comma seperated list of appenders. FileAppender was removed (no FS on SPOTs)
#   ConsoleAppender -- can only be viewed when SPOT client is connected via
#                      'ant run'
#   RecordStoreAppender -- stores log to record store, can be viewed by
#                      deploying and running the microlog jar file
#                      
#microlog.appender=net.sf.microlog.appender.RecordStoreAppender;net.sf.microlog.appender.ConsoleAppender;net.sf.microlog.midp.appender.FileConnectionAppender
microlog.appender=net.sf.microlog.appender.ConsoleAppender
# The maximum number of log entries to be held in the record store
#microlog.appender.RecordStoreAppender.maxLogEntries=500
# Note- time is only needed for console appender
# RecordStoreLogViewer will still display time without this variable set 
#
# The formatter to use
#   SimpleFormatter -- logs the debug level and message only
#   ConfigurableFormatter -- has the following options
#                      level     :  if true, logs the debug level
#                      message   :  if true, logs the debug message
#                      time      :  'date' to print entire date with message
#                                   'millis' to print time in milliseconds
#                                   NOTE: only needed for consoleviewer
#                                       date is always stored/viewed when
#                                       reading RMS log
#                      delimiter :  delimiter string between message parts
#microlog.formatter=net.sf.microlog.format.ConfigurableFormatter;
#microlog.formatter.ConfigurableFormatter.time=millis 
#microlog.formatter.ConfigurableFormatter.level=true 
#microlog.formatter.ConfigurableFormatter.message=true
#microlog.formatter.ConfigurableFormatter.delimiter=- 

# Set the pattern for the PatternFormatter
# microlog.formatter.PatternFormatter.pattern=%r %c %m %T
# This would print the relative logging time, the name of the Logger, the logged message and the Throwable object (if not null).
#
#  The available pattern conversions are:
# %c : prints the name of the Logger
# %d : prints the date (absolute time)
# %m : prints the logged message
# %P : prints the priority, i.e. Level of the message.
# %r : prints the relative time of the logging. (The first logging is done at time 0.)
# %t : prints the thread name.
# %T : prints the Throwable object.
# %% : prints the '%' sign.

microlog.appender=net.sf.microlog.core.appender.ConsoleAppender;
microlog.formatter=net.sf.microlog.core.format.PatternFormatter
microlog.formatter.PatternFormatter.name=MyFormatterName
microlog.formatter.PatternFormatter.pattern=%P [%d] %c--> %m %T

# End of file. Do not remove this line.

 

结果如下:

INFO [0] Hello3D--> start

 类似资料: