Skip to main content

log4j2 in java maven

1. Install the log4jcore maven dependency
2. Install the log4japi maven dependency

3. Then write the below code just make sure you are importing the the packages just check while you are importing import these two only I have imported other log and faced some problem thats why i am saying


package com.sandy;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


public class App {

    private static final Logger logger=LogManager.getLogger(App.class);
    public static void main(String[] args) {
       
        System.out.println("hello world");

        logger.trace("this is the trace message");
        logger.debug("this is the debug message");
        logger.info("this is the info message");   
        logger.warn("this is the debug message");
        logger.error("This is error logs");
        logger.fatal("fatal");
    }

}



4. Write now it will take the default configuration lets learn how we can use the configuration using an xml file

5. NOTE:- There are two ways to configure any framework either programatically by creating class and or by creating an xml right now we will look into using the xml approach.

6.Paste this in the file name log4j2.xml
7. Create a file in the source foder and try to close or refresh the project once
8. Now all the child class can use the same logger


<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
        </Console>
        <File name="FileAppender" fileName="application-${date:yyyyMMdd}.log" immediateFlush="false" append="true">
            <PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </File>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="ConsoleAppender" />
            <AppenderRef ref="FileAppender"/>
        </Root>
    </Loggers>
</Configuration>


9. So I will be creating either for each class logger object or we can just use the parent class logger object

This is one more examples

import com.sandy.testcases.*;

public class App {

   
    public static  Logger logger=LogManager.getLogger("devpinoyLogger");
    public static void main(String[] args) {
       
        TestClass.displayingLogs();
       

        logger.trace("this is the trace message");
        logger.debug("this is the debug message");
        logger.info("this is the info message");   
        logger.warn("this is the debug message");
        logger.error("This is error logs");
        logger.fatal("fatal");
       
       
        rbga.createRBGA();
       
       
    }
   
   

}

Comments

Popular posts from this blog

Clean Code Sonar Lint Java

Coding Guidelines 1. Sections of code should not be commented out (java:S125) CODE_SMELLCode smellMAJORMajor Programmers should not comment out code as it bloats programs and reduces readability. Unused code should be deleted and can be retrieved from source control history if required. 2. Standard outputs should not be used directly to log anything (java:S106) CODE_SMELLCode smellMAJORMajor When logging a message there are several important requirements which must be fulfilled: • The user must be able to easily retrieve the logs • The format of all logged message must be uniform to allow the user to easily read the log • Logged data must actually be recorded • Sensitive data must only be logged securely If a program directly writes to the standard outputs, there is absolutely no way to comply with those requirements. That's why defining and using a dedicated logger is highly recommended. Noncompliant Code Example System.out.println("My Message");  // Noncompliant Complia...

Xpath toughest

1. //span[contains(text(),'Please enter the comments!')]//following-sibling::div//input//following-sibling::textarea Some of the other Xpaths are 1. search RBGA //clicking on the new request //a[@title='Request a new workflow for RB General Approval Form']         @FindBy(xpath="//body[contains(@class,'ext-safari')]/form[contains(@name,'workOnIssueForm')]/table[contains(@class,'jiraform maxWidth')]/tbody/tr/td[contains(@class,'rb_formArea')]/fieldset[contains(@class,'rb_WorkON_FieldSet')]/table/tbody/tr/td/div[contains(@class,'rb_WorkON_FieldContainer')]/div[contains(@class,'rb_WorkON_FieldValueArea rb_WorkON_FieldValueArea_create rb_WorkON_ValueArea_Wide')]/table[contains(@class,'workonstdtable')]/tbody/tr/td/input[1]")     public WebElement tickMark;         @FindBy(xpath="//body[contains(@class,'ext-safari')]/form[contains(@name,'workOnIssueForm')]/table[contains(@class,...