How create log file using Log4j for jsp page












1















I want to create log file using Log4j. I want to create such logs.



"2018-10-31 21:05:51,481 - DEBUG - u click button"



I downloaded log4j-2.3-bin.



Added files log4j-api-2.3.jar and log4j-api-2.3.jar, clicked "build path" and create log4j.xml.



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


I am confused and do not quite understand how to finish.



I'm using IDE eclipse for web-development with tomcat 8.5.



MyFile.jps



<form  method="POST">
<input type="submit" name="Clickme" value ="buttonclick" >
<%
String button1Click = request.getParameter("buttonclick");
if(button1Click != null && button1Click.equals("buttonclick")){
%>
<p> click </p>
<%
}
%>
</form>









share|improve this question





























    1















    I want to create log file using Log4j. I want to create such logs.



    "2018-10-31 21:05:51,481 - DEBUG - u click button"



    I downloaded log4j-2.3-bin.



    Added files log4j-api-2.3.jar and log4j-api-2.3.jar, clicked "build path" and create log4j.xml.



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


    I am confused and do not quite understand how to finish.



    I'm using IDE eclipse for web-development with tomcat 8.5.



    MyFile.jps



    <form  method="POST">
    <input type="submit" name="Clickme" value ="buttonclick" >
    <%
    String button1Click = request.getParameter("buttonclick");
    if(button1Click != null && button1Click.equals("buttonclick")){
    %>
    <p> click </p>
    <%
    }
    %>
    </form>









    share|improve this question



























      1












      1








      1








      I want to create log file using Log4j. I want to create such logs.



      "2018-10-31 21:05:51,481 - DEBUG - u click button"



      I downloaded log4j-2.3-bin.



      Added files log4j-api-2.3.jar and log4j-api-2.3.jar, clicked "build path" and create log4j.xml.



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


      I am confused and do not quite understand how to finish.



      I'm using IDE eclipse for web-development with tomcat 8.5.



      MyFile.jps



      <form  method="POST">
      <input type="submit" name="Clickme" value ="buttonclick" >
      <%
      String button1Click = request.getParameter("buttonclick");
      if(button1Click != null && button1Click.equals("buttonclick")){
      %>
      <p> click </p>
      <%
      }
      %>
      </form>









      share|improve this question
















      I want to create log file using Log4j. I want to create such logs.



      "2018-10-31 21:05:51,481 - DEBUG - u click button"



      I downloaded log4j-2.3-bin.



      Added files log4j-api-2.3.jar and log4j-api-2.3.jar, clicked "build path" and create log4j.xml.



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


      I am confused and do not quite understand how to finish.



      I'm using IDE eclipse for web-development with tomcat 8.5.



      MyFile.jps



      <form  method="POST">
      <input type="submit" name="Clickme" value ="buttonclick" >
      <%
      String button1Click = request.getParameter("buttonclick");
      if(button1Click != null && button1Click.equals("buttonclick")){
      %>
      <p> click </p>
      <%
      }
      %>
      </form>






      java eclipse log4j tomcat8






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 5:58







      Owl_Livi

















      asked Nov 13 '18 at 5:31









      Owl_LiviOwl_Livi

      356




      356
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Create a dummy class, you need it to instantiate the logger:



          public class JspLoggerClass {}


          Import the logger and the class in your jsp:



          <%@ page import="org.apache.log4j.Logger, JspLoggerClass;" %>


          Instantiate and use:



            <%
          Logger logger = Logger.getLogger(JspLoggerClass.class);
          ...
          logger.info("message ...");
          %>


          EDITED



          I don't understand you very well. Using log4j you can write only from java code. That for in jsp you use the logger in scriptlet only. If you want to log the click event, that is javascript, so the only logger is console.log("...") but this prints on the browser console.
          When you click the button, there is no way to get the event in jsp scriptlet, this code is executed on the server. What you can do is submit the form, go to a servlet on your server and there log the event.



          EDITED 2



          OK, my fault, you are using 2.3 and that is log4j 2.
          So include in your project / classpath log4j-core and log4j-api jars. Import org.apache.logging.log4j.LogManager and org.apache.logging.log4j.Logger. No need to create dummy class, you could instantiate the logger with a string name like this:



          Logger logger = LogManager.getLogger("my jsp");


          And now just use the logger:



          logger.info("...");


          Sorry, I cannot post comments.



          I think you have an error in your log4j.xml, remove the dot before PatternLayout.






          share|improve this answer


























          • what version is it written for? I have version 2.3 and what you wrote is not working

            – Owl_Livi
            Nov 13 '18 at 10:31











          • Now error Invalid directive. How correct import log4j-api-2.3.jar? Do you have normal instruction? Please

            – Owl_Livi
            Nov 13 '18 at 11:15











          • in my file i don't have a dot.

            – Owl_Livi
            Nov 13 '18 at 11:21











          • On the line 8 in the file you've posted there is a dot. I don0t know how you can import log4j-api-2.3.jar. You must have a server, e.g. Tomcat, so just put the jar in your server/lib folder. If you deploy a war, make sure you have the jar in your WEB-INF/lib folder.

            – Evgeni Enchev
            Nov 13 '18 at 11:28











          • all code- work. But i can't see my logs.

            – Owl_Livi
            Nov 13 '18 at 11:40











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274420%2fhow-create-log-file-using-log4j-for-jsp-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Create a dummy class, you need it to instantiate the logger:



          public class JspLoggerClass {}


          Import the logger and the class in your jsp:



          <%@ page import="org.apache.log4j.Logger, JspLoggerClass;" %>


          Instantiate and use:



            <%
          Logger logger = Logger.getLogger(JspLoggerClass.class);
          ...
          logger.info("message ...");
          %>


          EDITED



          I don't understand you very well. Using log4j you can write only from java code. That for in jsp you use the logger in scriptlet only. If you want to log the click event, that is javascript, so the only logger is console.log("...") but this prints on the browser console.
          When you click the button, there is no way to get the event in jsp scriptlet, this code is executed on the server. What you can do is submit the form, go to a servlet on your server and there log the event.



          EDITED 2



          OK, my fault, you are using 2.3 and that is log4j 2.
          So include in your project / classpath log4j-core and log4j-api jars. Import org.apache.logging.log4j.LogManager and org.apache.logging.log4j.Logger. No need to create dummy class, you could instantiate the logger with a string name like this:



          Logger logger = LogManager.getLogger("my jsp");


          And now just use the logger:



          logger.info("...");


          Sorry, I cannot post comments.



          I think you have an error in your log4j.xml, remove the dot before PatternLayout.






          share|improve this answer


























          • what version is it written for? I have version 2.3 and what you wrote is not working

            – Owl_Livi
            Nov 13 '18 at 10:31











          • Now error Invalid directive. How correct import log4j-api-2.3.jar? Do you have normal instruction? Please

            – Owl_Livi
            Nov 13 '18 at 11:15











          • in my file i don't have a dot.

            – Owl_Livi
            Nov 13 '18 at 11:21











          • On the line 8 in the file you've posted there is a dot. I don0t know how you can import log4j-api-2.3.jar. You must have a server, e.g. Tomcat, so just put the jar in your server/lib folder. If you deploy a war, make sure you have the jar in your WEB-INF/lib folder.

            – Evgeni Enchev
            Nov 13 '18 at 11:28











          • all code- work. But i can't see my logs.

            – Owl_Livi
            Nov 13 '18 at 11:40
















          1














          Create a dummy class, you need it to instantiate the logger:



          public class JspLoggerClass {}


          Import the logger and the class in your jsp:



          <%@ page import="org.apache.log4j.Logger, JspLoggerClass;" %>


          Instantiate and use:



            <%
          Logger logger = Logger.getLogger(JspLoggerClass.class);
          ...
          logger.info("message ...");
          %>


          EDITED



          I don't understand you very well. Using log4j you can write only from java code. That for in jsp you use the logger in scriptlet only. If you want to log the click event, that is javascript, so the only logger is console.log("...") but this prints on the browser console.
          When you click the button, there is no way to get the event in jsp scriptlet, this code is executed on the server. What you can do is submit the form, go to a servlet on your server and there log the event.



          EDITED 2



          OK, my fault, you are using 2.3 and that is log4j 2.
          So include in your project / classpath log4j-core and log4j-api jars. Import org.apache.logging.log4j.LogManager and org.apache.logging.log4j.Logger. No need to create dummy class, you could instantiate the logger with a string name like this:



          Logger logger = LogManager.getLogger("my jsp");


          And now just use the logger:



          logger.info("...");


          Sorry, I cannot post comments.



          I think you have an error in your log4j.xml, remove the dot before PatternLayout.






          share|improve this answer


























          • what version is it written for? I have version 2.3 and what you wrote is not working

            – Owl_Livi
            Nov 13 '18 at 10:31











          • Now error Invalid directive. How correct import log4j-api-2.3.jar? Do you have normal instruction? Please

            – Owl_Livi
            Nov 13 '18 at 11:15











          • in my file i don't have a dot.

            – Owl_Livi
            Nov 13 '18 at 11:21











          • On the line 8 in the file you've posted there is a dot. I don0t know how you can import log4j-api-2.3.jar. You must have a server, e.g. Tomcat, so just put the jar in your server/lib folder. If you deploy a war, make sure you have the jar in your WEB-INF/lib folder.

            – Evgeni Enchev
            Nov 13 '18 at 11:28











          • all code- work. But i can't see my logs.

            – Owl_Livi
            Nov 13 '18 at 11:40














          1












          1








          1







          Create a dummy class, you need it to instantiate the logger:



          public class JspLoggerClass {}


          Import the logger and the class in your jsp:



          <%@ page import="org.apache.log4j.Logger, JspLoggerClass;" %>


          Instantiate and use:



            <%
          Logger logger = Logger.getLogger(JspLoggerClass.class);
          ...
          logger.info("message ...");
          %>


          EDITED



          I don't understand you very well. Using log4j you can write only from java code. That for in jsp you use the logger in scriptlet only. If you want to log the click event, that is javascript, so the only logger is console.log("...") but this prints on the browser console.
          When you click the button, there is no way to get the event in jsp scriptlet, this code is executed on the server. What you can do is submit the form, go to a servlet on your server and there log the event.



          EDITED 2



          OK, my fault, you are using 2.3 and that is log4j 2.
          So include in your project / classpath log4j-core and log4j-api jars. Import org.apache.logging.log4j.LogManager and org.apache.logging.log4j.Logger. No need to create dummy class, you could instantiate the logger with a string name like this:



          Logger logger = LogManager.getLogger("my jsp");


          And now just use the logger:



          logger.info("...");


          Sorry, I cannot post comments.



          I think you have an error in your log4j.xml, remove the dot before PatternLayout.






          share|improve this answer















          Create a dummy class, you need it to instantiate the logger:



          public class JspLoggerClass {}


          Import the logger and the class in your jsp:



          <%@ page import="org.apache.log4j.Logger, JspLoggerClass;" %>


          Instantiate and use:



            <%
          Logger logger = Logger.getLogger(JspLoggerClass.class);
          ...
          logger.info("message ...");
          %>


          EDITED



          I don't understand you very well. Using log4j you can write only from java code. That for in jsp you use the logger in scriptlet only. If you want to log the click event, that is javascript, so the only logger is console.log("...") but this prints on the browser console.
          When you click the button, there is no way to get the event in jsp scriptlet, this code is executed on the server. What you can do is submit the form, go to a servlet on your server and there log the event.



          EDITED 2



          OK, my fault, you are using 2.3 and that is log4j 2.
          So include in your project / classpath log4j-core and log4j-api jars. Import org.apache.logging.log4j.LogManager and org.apache.logging.log4j.Logger. No need to create dummy class, you could instantiate the logger with a string name like this:



          Logger logger = LogManager.getLogger("my jsp");


          And now just use the logger:



          logger.info("...");


          Sorry, I cannot post comments.



          I think you have an error in your log4j.xml, remove the dot before PatternLayout.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 11:19

























          answered Nov 13 '18 at 8:06









          Evgeni EnchevEvgeni Enchev

          3115




          3115













          • what version is it written for? I have version 2.3 and what you wrote is not working

            – Owl_Livi
            Nov 13 '18 at 10:31











          • Now error Invalid directive. How correct import log4j-api-2.3.jar? Do you have normal instruction? Please

            – Owl_Livi
            Nov 13 '18 at 11:15











          • in my file i don't have a dot.

            – Owl_Livi
            Nov 13 '18 at 11:21











          • On the line 8 in the file you've posted there is a dot. I don0t know how you can import log4j-api-2.3.jar. You must have a server, e.g. Tomcat, so just put the jar in your server/lib folder. If you deploy a war, make sure you have the jar in your WEB-INF/lib folder.

            – Evgeni Enchev
            Nov 13 '18 at 11:28











          • all code- work. But i can't see my logs.

            – Owl_Livi
            Nov 13 '18 at 11:40



















          • what version is it written for? I have version 2.3 and what you wrote is not working

            – Owl_Livi
            Nov 13 '18 at 10:31











          • Now error Invalid directive. How correct import log4j-api-2.3.jar? Do you have normal instruction? Please

            – Owl_Livi
            Nov 13 '18 at 11:15











          • in my file i don't have a dot.

            – Owl_Livi
            Nov 13 '18 at 11:21











          • On the line 8 in the file you've posted there is a dot. I don0t know how you can import log4j-api-2.3.jar. You must have a server, e.g. Tomcat, so just put the jar in your server/lib folder. If you deploy a war, make sure you have the jar in your WEB-INF/lib folder.

            – Evgeni Enchev
            Nov 13 '18 at 11:28











          • all code- work. But i can't see my logs.

            – Owl_Livi
            Nov 13 '18 at 11:40

















          what version is it written for? I have version 2.3 and what you wrote is not working

          – Owl_Livi
          Nov 13 '18 at 10:31





          what version is it written for? I have version 2.3 and what you wrote is not working

          – Owl_Livi
          Nov 13 '18 at 10:31













          Now error Invalid directive. How correct import log4j-api-2.3.jar? Do you have normal instruction? Please

          – Owl_Livi
          Nov 13 '18 at 11:15





          Now error Invalid directive. How correct import log4j-api-2.3.jar? Do you have normal instruction? Please

          – Owl_Livi
          Nov 13 '18 at 11:15













          in my file i don't have a dot.

          – Owl_Livi
          Nov 13 '18 at 11:21





          in my file i don't have a dot.

          – Owl_Livi
          Nov 13 '18 at 11:21













          On the line 8 in the file you've posted there is a dot. I don0t know how you can import log4j-api-2.3.jar. You must have a server, e.g. Tomcat, so just put the jar in your server/lib folder. If you deploy a war, make sure you have the jar in your WEB-INF/lib folder.

          – Evgeni Enchev
          Nov 13 '18 at 11:28





          On the line 8 in the file you've posted there is a dot. I don0t know how you can import log4j-api-2.3.jar. You must have a server, e.g. Tomcat, so just put the jar in your server/lib folder. If you deploy a war, make sure you have the jar in your WEB-INF/lib folder.

          – Evgeni Enchev
          Nov 13 '18 at 11:28













          all code- work. But i can't see my logs.

          – Owl_Livi
          Nov 13 '18 at 11:40





          all code- work. But i can't see my logs.

          – Owl_Livi
          Nov 13 '18 at 11:40


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274420%2fhow-create-log-file-using-log4j-for-jsp-page%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          The Sandy Post

          Danny Elfman

          Pages that link to "Head v. Amoskeag Manufacturing Co."