Results 1 to 5 of 5
  1. #1
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Issues with Log4j2 in Netbeans

    Hello.

    I am trying to add Log4j2 to my project.

    First, I created an XML configuration file and added it to my Project:

    Issues with Log4j2 in Netbeans-img3.png

    Issues with Log4j2 in Netbeans-img2.png

    Issues with Log4j2 in Netbeans-img1.png

    Then, I used it in my Main class:

    Java Code:
    public class Main 
    {
        private final static Logger LOGGER = LogManager.getLogger(iGoDispatch.class);
        
        public static void main(String[] args) 
        {
            SwingUtilities.invokeLater(() -> 
            {
                LOGGER.debug("Debug Message Logged !!!");
                LOGGER.info("Info Message Logged !!!");
                LOGGER.error("Error Message Logged !!!", new NullPointerException("NullError"));
                
                // Check whther Initial Warning must be shown
                Main main = new Main();
                main.startApplication();
            });
        }
    }
    However, I believe that my configuration file is not properly loaded. I receive the following message in my Console, which does not correspond to the pattern written in the xml file:

    Issues with Log4j2 in Netbeans-img4.png

    And the log file is not saved in my Application/config/ folder.

    Thank you!
    Last edited by igorland; 02-18-2018 at 06:20 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,948
    Rep Power
    30

    Default Re: Issues with Log4j2 in Netbeans

    On what source line is the NPE happening?
    What variable is causing the NPE?

    Note: Pasting images of source or messages makes it impossible to copy text into a response.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: Issues with Log4j2 in Netbeans

    Norm. It is called on Line 11. It is OK, I believe, since I do not have any errors. The problem is not that I have that NPE exception. The point is that my Console message is not in accordance with the pattern written in Log4j2.xlm, and that a log file is not created. So my question is: did I install everything right?
    Thank you!

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,948
    Rep Power
    30

    Default Re: Issues with Log4j2 in Netbeans

    Sorry, I thought this was a programming problem.
    I'll move it to the IDE section.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    igorland is offline Member
    Join Date
    Jan 2013
    Posts
    89
    Rep Power
    0

    Default Re: Issues with Log4j2 in Netbeans

    Tried to make it work for days and days. Dropped that idea and created my own class.

    Java Code:
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import models.AppSingleton;
    
    
    /**
     * This class is a singleton that creates a Log file and writes to it.
     * @author Igor Tupitsyn
     */
    public class IGALogger 
    {
        /** Instance of Singleton. */
        private static IGALogger instance = null;
        /** Name of the Log file */
        String cfgFile = "LogIGD.txt";
        
        
        /**
        * Creates an instance of the Singleton class.
        * @return Singleton instance
        */
        public static IGALogger getInstance() 
        {
            if(instance == null) 
            {
                instance = new IGALogger();
            }
            return instance;
        }
        
        
        
        
        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++
        //
        // METHODS TO CREATE LOG FILES
        //
        // ++++++++++++++++++++++++++++++++++++++++++++++++++++++
        
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        
        /**
         * Check if a Log file exists and, if not, creates the file.
         * The file is located in subfolder "/config".
         */
        public void createLogFile()
        {
            // Directory and file location
            File workingDirectory = new File(System.getProperty("user.dir"));
            String cfgDirectory = workingDirectory + "/config/";
            String filePathString = cfgDirectory+cfgFile;
                
    
            try 
            {
                // Check of the directory exists already
                File directory = new File(cfgDirectory);
                if (! directory.exists())
                {
                    // If not, create it.
                    directory.mkdir();
                }
                
                // Check if the Log file exists already and, if so, delete it
                File f = new File(filePathString);
                if(f.exists() && !f.isDirectory()) 
                { 
                    f.delete();
                }
                
                // Create a new file
                f.createNewFile();
                
                // Date and time
                String dateStamp = new SimpleDateFormat("MM/dd/yyyy").format(Calendar.getInstance().getTime());
                String timeStamp = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
                
                String str = "Log.txt for Application\n"
                        + "Log created on "+dateStamp+" at "+timeStamp+"\n\n"
                        + "This file is generated automatically by Application. It includes information "
                        + "about any errors that might have occurred at runtime of this program.\n" 
                        + "A new Log file is generated every time you start Application.\n\n"
                        + "=============================================================================\n\n";
                BufferedWriter writer = new BufferedWriter(new FileWriter(f, true));
                writer.append(str);
                
                // Operating System
                String nameOS = "Operating System: " + System.getProperty("os.name")+"\n";
                String versionOS = "Operating System Version: " + System.getProperty("os.version")+"\n";
                String architectureOS = "Operating System Architecture: " + System.getProperty("os.arch")+"\n";
                String processors = "Available processors (cores): " + Runtime.getRuntime().availableProcessors()+"\n";
                writer.append(nameOS);
                writer.append(versionOS);
                writer.append(architectureOS);
                writer.append(processors);
                
                writer.close();
    
            } 
            catch (IOException e) 
            {
                System.out.println("Log File not created");
    	}
        }
        
        
        /**
         * Creates Information Log message and adds it to the Log.txt file.
         * @param message Log Message
         */
        public void addLogInfo(String message)
        {
            // Directory and file location
            File workingDirectory = new File(System.getProperty("user.dir"));
            String cfgDirectory = workingDirectory + "/config/";
            String filePathString = cfgDirectory+cfgFile;
    
            try 
            {
                String lineNumber = String.valueOf(Thread.currentThread().getStackTrace()[2].getLineNumber() );
                String className = Thread.currentThread().getStackTrace()[2].getClassName();
                String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
                
                File f = new File(filePathString);
                if(f.exists() && !f.isDirectory()) 
                { 
                    String str = "[INFO] - "+lineNumber+" - "+className+" - "+methodName+" - "+message+"\n";
                    BufferedWriter writer = new BufferedWriter(new FileWriter(f, true));
                    writer.append(str);
                    writer.close();
                }
            } 
            catch (IOException e) 
            { 
                System.out.println("Log File not created");
    	}
        }
        
        
        /**
         * Creates Warning Log message and adds it to the Log.txt file.
         * @param message Log message
         */
        public void addLogWarn(String message)
        {
            // Directory and file location
            File workingDirectory = new File(System.getProperty("user.dir"));
            String cfgDirectory = workingDirectory + "/config/";
            String filePathString = cfgDirectory+cfgFile;
    
            try 
            {
                String lineNumber = String.valueOf(Thread.currentThread().getStackTrace()[2].getLineNumber() );
                String className = Thread.currentThread().getStackTrace()[2].getClassName();
                String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
                
                File f = new File(filePathString);
                if(f.exists() && !f.isDirectory()) 
                { 
                    String str = "[WARN] - "+lineNumber+" - "+className+" - "+methodName+" - "+message+"\n";
                    BufferedWriter writer = new BufferedWriter(new FileWriter(f, true));
                    writer.append(str);
                    writer.close();
                }
            } 
            catch (IOException e) 
            {
                System.out.println("Log File not created");
    	}
        }
        
        
        /**
         * Creates Error Log message and adds it to the Log.txt file.
         * @param message Log message
         */
        public void addLogError(String message)
        {
            // Directory and file location
            File workingDirectory = new File(System.getProperty("user.dir"));
            String cfgDirectory = workingDirectory + "/config/";
            String filePathString = cfgDirectory+cfgFile;
    
            try 
            {
                String lineNumber = String.valueOf(Thread.currentThread().getStackTrace()[2].getLineNumber() );
                String className = Thread.currentThread().getStackTrace()[2].getClassName();
                String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
                
                File f = new File(filePathString);
                if(f.exists() && !f.isDirectory()) 
                { 
                    String str = "[ERROR] - "+lineNumber+" - "+className+" - "+methodName+" - "+message+"\n";
                    BufferedWriter writer = new BufferedWriter(new FileWriter(f, true));
                    writer.append(str);
                    writer.close();
                }
            } 
            catch (IOException e) 
            {
                System.out.println("Log File not created");
    	}
        }
        
        // </editor-fold>
    }
    Cheers.

Similar Threads

  1. Replies: 16
    Last Post: 07-02-2014, 01:02 PM
  2. issues in threading
    By mkarthik90 in forum Java Servlet
    Replies: 9
    Last Post: 03-06-2012, 02:17 PM
  3. GUI Issues
    By larry_d1990 in forum Advanced Java
    Replies: 2
    Last Post: 03-17-2011, 02:35 PM
  4. jdk issues
    By artemff in forum New To Java
    Replies: 3
    Last Post: 01-02-2010, 03:18 AM
  5. Issues with Jva I.O
    By Annatar01 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •