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.

std::cerr<<__DATE__<<'\t'<<msg<<'\n'
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
import "log"
log.Println(msg)
import groovy.util.logging.Slf4j
@Slf4j
class X {
    def m(String message) {
        log.debug(message)
    }
}
console.log(Date(), msg);
console.error(Date(), msg);
import java.util.logging.Logger;
Logger LOGGER = Logger.getLogger(MyClass.class.getName());

LOGGER.info(msg);
sysutils
writeln(DateToStr(Now), #32, msg); //defaults to stdout
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)

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms