自定义日志记录的Andr​​oid应用程序 [英] Custom logging for Android Application

查看:133
本文介绍了自定义日志记录的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义记录器的Andr​​oid应用程序。 日志记录应该在一个单独的线程来完成的应用程序产生大量的信息。我不希望使用Android日志,因为我需要写在日志中特定的格式。多线程将被写在同一时间的日志文件,所以我使用了一个队列,以保持日志信息

I want to create a custom logger for android application. The logging should be done in a separate thread as the application generates lots of information. I don't want to use the Android logs since i need to write the logs in a specific format. Multiple threads will be writing to the log file at the same time, so i have used a queue to keep the log messages

下面是code,我有

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) 
        {                
        }
    }
}

有什么事情不对的code?或者更好的方式来建立一个日志队列

Is there any thing wrong with this code? or a better way to create a logging queue

谢谢, Anuj

推荐答案

已经内置同步关切。您使用的等待,通知,以及同步的Java BlockingQueue实现是多余的,没有必要的。

Java BlockingQueue implementations already have synchronization concerns built in. Your use of wait, notify, and synchronized is redundant and not needed.

试着模仿生产者/消费者的例子给了的的BlockingQueue的javadoc

Try mimicking the Producer/Consumer example giving in the BlockingQueue javadoc

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");
  }
}

这篇关于自定义日志记录的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆