小编Anu*_*pta的帖子

Android应用程序的自定义日志

我想为Android应用程序创建一个自定义记录器.当应用程序生成大量信息时,应在单独的线程中完成日志记录.我不想使用Android日志,因为我需要以特定格式编写日志.多个线程将同时写入日志文件,因此我使用队列来保留日志消息

这是我的代码

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 …
Run Code Online (Sandbox Code Playgroud)

java logging android

13
推荐指数
1
解决办法
5269
查看次数

标签 统计

android ×1

java ×1

logging ×1