问题
I know that it is possible that every line you print can be tagged with date (and also saved as a log file).
For example in Minecraft:
[16:21:43 INFO]: Minecraft Launcher 1.3.9 (through bootstrap 5) started on windows...
How do I do that? Maybee with Logger? Or are external librarys required?
回答1:
It's possible to show dates prefixed in your String by calling System.out.println
- Create your custom
PrintStream
class - Override
println
method to prefix the String with the date - Set
System.setOut
with your customPrintStream
Custom stream:
public class MyPrintStream extends PrintStream {
public MyPrintStream(OutputStream out) {
super(out);
}
@Override
public void println(String string) {
Date date = new Date();
super.println("[" + date.toString() + "] " + string);
}
}
Test class:
public static void main(String[] args) {
System.setOut(new MyPrintStream(System.out));
System.out.println("Hello World!");
System.out.println("Yellow World!");
}
Output:
[Mon Feb 10 21:10:14 IST 2014] Hello World!
[Mon Feb 10 21:10:14 IST 2014] Yellow World!
回答2:
These aren't done directly through println, they are done using loggers. Note the INFO level as well as the date.
private static final Logger LOGGER = Logger.getLogger(X.getClass.getName());
Then
LOGGER.info("Hi");
LOGGER.debug("This is a debug message");
etc.
The most common loggers are either Apache log4j or the built in logger in java.util.logging.
回答3:
While the answer by PopoFibo is correct, clever, and specifically answers the question, the larger answer is: Use a logging framework. A logging framework will automatically insert the current date-time and do much more as well.
Usually we use System.out.println() just for quick-and-dirty checking of values while programming. The more organized way to approach getting information out of your program is using a logging framework (or JMX – Java Management Extensions, but that’s another topic altogether).
log4j
In the old days, the log4j framework was a popular framework, and one of the first to create a slick logging system. Currently an overhaul of log4j is underway, called Apache Log4j 2.
java.util.logging
Later, Sun added a logging facility to Java. While useful, many folks consider it less-than-optimal.
SLF4J
The creator of log4j later went on to create a successor to his previous invention. He broke it apart into (1) a façade, an interface-based API, and (2) any number of implementations that actually do the logging. The idea is that you could plugin or replace various implementations without changing your own code base.
The façade is called SLF4J (Simple Logging Façade for Java). SLF4J comes bundled with a couple very simple implementations. But generally you will want to plugin more robust, flexible, or specialized implementations.
Several adapters are available to use other logging frameworks that may not be aware of SLF4J such as log4j or java.util.Logging.
Many libraries use SLF4J for their internal logging so that anyone using that library can plug in their own particular choice of logging framework underneath.
LogBack
The creator of SLF4J also created his own full implementation of SLF4J, called LogBack.
Uses
One of the main points to using these logging frameworks is flexibility. You can configure them to send messages to System.out
, System.err
, text files, databases, and other destinations.
来源:https://stackoverflow.com/questions/21681066/show-date-in-system-out-println-statement-automatically