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.
New 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
Console.WriteLine($"[{DateTime.Now}] {msg}");
import std.experimental.logger;
log(msg);
print('${DateTime.now()} 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
import "log"
log.Println(msg)
import groovy.util.logging.Slf4j
@Slf4j
class X {
    def m(String message) {
        log.debug(message)
    }
}
console.error(Date(), msg);
console.log(Date(), msg);
import java.util.logging.Logger;
Logger LOGGER = Logger.getLogger(MyClass.class.getName());

LOGGER.info(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);
uses sysutils;
writeln(DateToStr(Now), #32, msg);
use Time::Piece qw(localtime);
my $logline = sprintf "%s %s\n", localtime->strftime('%F %T'), $msg;

print $logline; # stdout
warn $logline; # stderr
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)
require 'logger'
logger = Logger.new('logfile.log') # or STDOUT or STDERR
logger.info(msg)
eprintln!("[{}] {}", humantime::format_rfc3339_seconds(std::time::SystemTime::now()), msg);
My.Application.Log.WriteEntry(msg,TraceEventType.Verbose)