utils Logging

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

utils Logging

Postby stilwalli » Wed Aug 27, 2008 1:40 pm

Hi ,

I am trying to log to file using java.util.logging. My code happens to work. But I read it in one of the posts, the the files hsould be created in some other manner. So I was not sure if this the correct way of doing it.

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
Logger logger = Logger.getLogger("Test");
Handler handler = null;
try {
logger.setLevel(Level.WARNING);
handler = new FileHandler("/data/data/org.hsc.learning.main/files/droid.log");
handler.setFormatter(new Formatter() {
public String format(LogRecord record) {
return record.getMessage()+ "\n";
}
});
logger.addHandler(handler);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LogRecord record = new LogRecord(Level.INFO, "Terms=Yes");
logger.log(record);
record = new LogRecord(Level.FINE, "Terms=Yes");
logger.log(record);
record = new LogRecord(Level.FINER, "Terms=nO");
logger.log(record);
record = new LogRecord(Level.FINEST, "Terms=1");
logger.log(record);
record = new LogRecord(Level.SEVERE, "Terms=2");
logger.log(record);
record = new LogRecord(Level.WARNING, "Terms=3");
logger.log(record);
Handler[] handlers = Logger.getLogger("Test").getHandlers();
for (int i = 0; i < handlers.length; i++) {
System.out.println(handlers[i].getLevel().getName());
}
Log.i("HelloAndroid", "123456789");
tv.setText("Hello, Android");
setContentView(tv);
}

PS: The code works. But is this the correct way of writing log to file.

Thanks
Shashank
stilwalli
Developer
Developer
 
Posts: 35
Joined: Mon Jul 21, 2008 8:13 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests