Logo

Programming-Idioms

Print message msg, prepended by current date and time.

Explain what behavior is idiomatic: to stdout or stderr, and what the date format is.
Implementation
Java

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Java implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
import "log"
log.Println(msg)
require 'logger'
logger = Logger.new('logfile.log') # or STDOUT or STDERR
logger.info(msg)
import sys, logging
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(asctime)-15s %(message)s")
logger = logging.getLogger('NAME OF LOGGER')

logger.info(msg)
uses sysutils;
writeln(DateToStr(Now), #32, msg);
import std.experimental.logger;
log(msg);
integer :: value(8)
msg = "asdf"
call date_and_time (values=value)
write (unit=*,fmt='(I4,"-",I2.2,"-",I2.2," ",I2.2,":",I2.2,".",I4.4,": ",A)') value(1:3), value(5:7), msg
use Time::Piece qw(localtime);
my $logline = sprintf "%s %s\n", localtime->strftime('%F %T'), $msg;

print $logline; # stdout
warn $logline; # stderr
My.Application.Log.WriteEntry(msg,TraceEventType.Verbose)
eprintln!("[{}] {}", humantime::format_rfc3339_seconds(std::time::SystemTime::now()), msg);
import groovy.util.logging.Slf4j
@Slf4j
class X {
    def m(String message) {
        log.debug(message)
    }
}
console.log(Date(), msg);
console.error(Date(), msg);
Console.WriteLine($"[{DateTime.Now}] {msg}");
print('${DateTime.now()} msg');
long unixEpoch = System.currentTimeMillis();
String dateAndTime = String.format("%1$tY-%1$tm-%1$td %1$tI:%1$tM:%1$tS", unixEpoch);
System.out.printf("%s: %s%n", dateAndTime, msg);
System.err.printf("%s: %s%n", dateAndTime, msg);