• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use logger and prints time and date  RSS feed

 
Isaac Caleb Smith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone!

Can someone help me do my project? It is my first time to use logge function and I need to add Logger function in the catch block that prints time, date and description of the what had happened.

Please help! It would be much appreciated Thanks in advance for those who will help!


 
Isaac Caleb Smith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And followup question is, how to test if my catch block does work?

From my understanding, I should input string index equal to 0 to test it? Am I right?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have the catch-block at all? The code inside the try-block will never throw a StringIndexOutOfBoundsException.
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) I would start examining Javadoc for classes Date and SimpleDateFormat for information about current date and time. Then take a look at class Exception for how to get the reason why exception was thrown. Finally look at methods of Logger class (especially log()).
2) I don't see a place where exception could be thrown in the try block.
 
Isaac Caleb Smith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Why do you have the catch-block at all? The code inside the try-block will never throw a StringIndexOutOfBoundsException.


Becos outputString will be inserted to database and there is a max length for it.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isaac Caleb Smith wrote:
Jesper de Jong wrote:Why do you have the catch-block at all? The code inside the try-block will never throw a StringIndexOutOfBoundsException.


Becos outputString will be inserted to database and there is a max length for it.


Then you should take an action during the input
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in the code you posted, there is nothing in the try-block which can throw this exception. Is the code not yet complete? Are you going to add putting it in the database inside the try-block?

Anyway, if you want to do logging, use a logging framework. Java has a built-in logging framework, see the API documentation of the package java.util.logging. In practice, almost nobody seems to use this however, because there were already other de-facto standard logging libraries before it was added to the standard Java API.

I would use SLF4J, which is a logging API that you can plug different logging library implementations into, for example Logback, Log4J or even java.util.logging. When you use SLF4J, it would look something like this:


The logging framework automatically adds timestamps etc. When the exception would occur, the logging would look something like this:

16:06:37,799 ERROR [main] InputUtil:33 - Exception while deleting special characters
java.lang.StringIndexOutOfBoundsException
at example.InputUtil.main(InputUtil.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
 
Campbell Ritchie
Marshal
Posts: 56597
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of that array. You have made it public static final as if it were a global constant. There is nothing to stop me writing things like thisNow your array reads {"A", "B", "C", etc}.

The reason is that arrays are implicitly mutable and by making it public you have exposed it to alterations. Try this instead
 
Isaac Caleb Smith
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:But in the code you posted, there is nothing in the try-block which can throw this exception. Is the code not yet complete? Are you going to add putting it in the database inside the try-block?

Anyway, if you want to do logging, use a logging framework. Java has a built-in logging framework, see the API documentation of the package java.util.logging. In practice, almost nobody seems to use this however, because there were already other de-facto standard logging libraries before it was added to the standard Java API.

I would use SLF4J, which is a logging API that you can plug different logging library implementations into, for example Logback, Log4J or even java.util.logging. When you use SLF4J, it would look something like this:


The logging framework automatically adds timestamps etc. When the exception would occur, the logging would look something like this:

16:06:37,799 ERROR [main] InputUtil:33 - Exception while deleting special characters
java.lang.StringIndexOutOfBoundsException
at example.InputUtil.main(InputUtil.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)



Thanks for the reply! been a long time since I login

I just want to ask, 16:06:37 is the time right? How about date? Is there an automatic date whenever the logging occurs? or Should I use SimpleDateFormat for that and place it inside the log?

And also, what is the difference between when you use 'e' alone and e.getMessage()?


Thanks in advance for the answer!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!