Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #145 Print log line with datetime

Print message msg, prepended by current date and time.

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

import "log"
log.Println(msg)
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 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);
import static java.lang.System.currentTimeMillis;
import static java.lang.System.out;
out.printf("%tc:  `%s`%n", currentTimeMillis(), 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)

New implementation...