Java Android应用程序的自定义日志记录
我想为android应用程序创建一个自定义记录器。 日志应该在单独的线程中完成,因为应用程序会生成大量信息。我不想使用Android日志,因为我需要以特定的格式编写日志。多个线程将同时写入日志文件,因此我使用了一个队列来保存日志消息 这是我的代码Java Android应用程序的自定义日志记录,java,android,logging,Java,Android,Logging,我想为android应用程序创建一个自定义记录器。 日志应该在单独的线程中完成,因为应用程序会生成大量信息。我不想使用Android日志,因为我需要以特定的格式编写日志。多个线程将同时写入日志文件,因此我使用了一个队列来保存日志消息 这是我的代码 Queue<LogEntry> logQueue = new LinkedBlockingQueue<LogEntry>(); LogWritterThread logWritterThread = new LogWritter
Queue<LogEntry> logQueue = new LinkedBlockingQueue<LogEntry>();
LogWritterThread logWritterThread = new LogWritterThread();
// to queue the log messages
public void QueueLogEntry(String message)
{
LogEntry le = new LogEntry(message);
{
logQueue.add(le);
logQueue.notifyAll();
}
logWritterThread.start();
}
class LogWritterThread extends Thread
{
public void run()
{
try
{
while(true)
{
//thread waits until there are any logs to write in the queue
if(logQueue.peek() == null)
synchronized(logQueue){
logQueue.wait();
}
if(logQueue.peek() != null)
{
LogEntry logEntry;
synchronized(logQueue){
logEntry = logQueue.poll();
}
// write the message to file
}
if(Thread.interrupted())
break;
}
}
catch (InterruptedException e)
{
}
}
}
Queue logQueue=new LinkedBlockingQueue();
LogWriterThread LogWriterThread=新的LogWriterThread();
//将日志消息排入队列
public void QueueLogEntry(字符串消息)
{
LogEntry le=新日志条目(消息);
{
logQueue.add(le);
logQueue.notifyAll();
}
logwriterthread.start();
}
类LogWriterThread扩展线程
{
公开募捐
{
尝试
{
while(true)
{
//线程等待,直到队列中有任何要写入的日志
if(logQueue.peek()==null)
已同步(日志队列){
logQueue.wait();
}
if(logQueue.peek()!=null)
{
日志条目日志条目;
已同步(日志队列){
logEntry=logQueue.poll();
}
//将消息写入文件
}
if(Thread.interrupted())
打破
}
}
捕捉(中断异常e)
{
}
}
}
这个代码有什么问题吗?还是创建日志队列的更好方法
谢谢,
Anuj我会使用处理程序——我喜欢处理程序,因为它们实现了队列和消息线程,所有这些都是线程安全的。这意味着您可以创建一个扩展处理程序的类,并在整个项目中使用该类。使用带有处理程序的几行代码,您可以大幅缩减现有代码 谷歌的文档: 这是一个非常简单的例子: 这是一个更好的例子,因为他们使用“msg.what”来决定要做什么(不同级别的日志记录需要这样做):
Java BlockingQueue实现已经内置了同步问题。您对wait、notify和synchronized的使用是多余的,不需要 试着模仿生产商/消费者在
类日志条目{
私有最终字符串消息;
日志条目(字符串消息){
消息=消息;
}
}
类日志生成器{
私有最终阻塞队列;
日志生成器(阻塞队列q){
队列=q;
}
公共无效日志(字符串msg){
put(新日志条目(msg));
}
}
类LogConsumer实现Runnable{
私有最终阻塞队列;
LogConsumer(阻塞队列q){
队列=q;
}
公开募捐{
试一试{
while(true){
LogEntry=queue.take();
//对入口做些什么
}
}捕获(中断异常例外){
//处理
}
}
}
班级设置{
公共静态void main(字符串[]args){
BlockingQueue=新建LinkedBlockingQueue();
LogConsumer c=新的LogConsumer(队列);
新线程(c.start();
LogProducer p=新的LogProducer(队列);
p、 日志(“异步”);
p、 日志(“日志”);
}
}
谢谢您的帮助。。。不幸的是,我不能把它们都标为答案
class LogEntry {
private final String message;
LogEntry(String msg) {
message = msg;
}
}
class LogProducer {
private final BlockingQueue<LogEntry> queue;
LogProducer(BlockingQueue<LogEntry> q) {
queue = q;
}
public void log(String msg) {
queue.put(new LogEntry(msg));
}
}
class LogConsumer implements Runnable {
private final BlockingQueue<LogEntry> queue;
LogConsumer(BlockingQueue<LogEntry> q) {
queue = q;
}
public void run() {
try {
while(true) {
LogEntry entry = queue.take();
// do something with entry
}
} catch(InterruptedException ex) {
// handle
}
}
}
class Setup {
public static void main(String[] args) {
BlockingQueue<LogEntry> queue = new LinkedBlockingQueue<LogEntry>();
LogConsumer c = new LogConsumer(queue);
new Thread(c).start();
LogProducer p = new LogProducer(queue);
p.log("asynch");
p.log("logging");
}
}