Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3999088
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:36:42+00:00 2026-05-20T07:36:42+00:00

I want to create a custom logger for android application. The logging should be

  • 0

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

Here is the code that i have

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

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

Thanks,
Anuj

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:36:43+00:00Added an answer on May 20, 2026 at 7:36 am

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

    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");
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to know if i can create a custom google maps application,on which
I want to create a custom toolbar control (descendant TToolBar) which should have some
I want to create a custom control in C#. But every time I have
I want to create a custom MSBuild task that changes my .cs files before
If you want to create a custom attribute for MS test (say [Repeat(3)] how
In C# if I want to create a Custom Event you do something like
I want to create extra rules in FXCop. Custom Rules to help ensure specific
I want to create a Java application bundle for Mac without using Mac. According
I'm creating a logger application thing to learn WPF, and I want new messages
I want to create custom WPF control that has a single child control inside.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.