Logo

Programming-Idioms

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

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 sys, logging
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(asctime)-15s %(message)s")
logger = logging.getLogger('NAME OF LOGGER')

logger.info(msg)

Default output is stderr.

Date format is ISO 8601.
Console.WriteLine($"[{DateTime.Now}] {msg}");

Default behavior of Console.WriteLine is stdout. DateTime should respect system culture defaults.

New implementation...