How to Customize the time format for Python logging?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Python logging timestamps come from the formatter, not from the logger itself. To customize the time format, include %(asctime)s in the log format and pass a datefmt string, or override the formatter's time conversion behavior if you need something more specialized.
Basic datefmt Usage
The standard way to control the timestamp is logging.Formatter.
The datefmt argument uses strftime directives. That lets you switch between formats such as:
- '
%Y-%m-%d %H:%M:%S' - '
%d/%m/%Y %I:%M:%S %p' - '
%H:%M:%S'
If %(asctime)s is not present in the format string, the customized time format will not appear.
Customizing basicConfig
If your application uses logging.basicConfig, you can still provide the same parameters directly there.
This is the quickest approach for scripts and small applications.
Including Milliseconds
A common requirement is to keep milliseconds in the output. The default formatter uses a comma-separated millisecond suffix, but you can format it more explicitly.
This produces a timestamp shaped like 2026-03-07 14:05:09.123.
Using UTC Instead of Local Time
By default, logging formats timestamps in local time. If you want UTC, set the formatter's converter to time.gmtime.
This is useful for logs that are aggregated across servers in different time zones.
dictConfig and Application-Wide Formatting
Larger applications often configure logging with logging.config.dictConfig instead of building handlers inline. The same timestamp controls still apply there.
This is the cleanest way to keep a consistent timestamp format across modules, worker processes, or framework components.
Full Control With a Custom Formatter
If datefmt is not enough, override formatTime. That lets you generate ISO 8601 strings, timezone-aware values, or any other custom representation.
This approach is ideal when you want a precise API-friendly format and do not want to depend on strftime limitations.
Watch Out for Multiple Handlers
Each handler has its own formatter. If your console logs and file logs need different timestamp formats, configure them separately.
This is often cleaner than trying to force one global format everywhere.
Common Pitfalls
The most common mistake is setting datefmt but forgetting %(asctime)s in the main format string. Without %(asctime)s, the timestamp field is never emitted.
Another issue is expecting datefmt alone to control milliseconds. If you want a custom millisecond style, combine %(asctime)s with %(msecs)03d or override formatTime.
Developers also sometimes use local time unintentionally in distributed systems. If the logs will be merged from several machines, UTC is usually safer.
Finally, if logs are duplicated, the problem is probably multiple handlers or logger propagation, not the time format itself.
Summary
- Use
logging.Formatterwith%(asctime)sanddatefmtfor normal timestamp customization. - '
basicConfigsupports the same formatting options for simple setups.' - Add
%(msecs)03dif you want explicit milliseconds. - Set
formatter.converter = time.gmtimefor UTC output. - Override
formatTimewhen you need full control over the timestamp format.
Related reading
- How to debug Kubernetes CreateContainerConfigError
- How to define a variable in a Dockerfile?
- How to delete a deployment / image in kubernetes
- How to delete a docker image?
- How to deal with SettingWithCopyWarning in Pandas
- How to deal with SettingWithCopyWarning in Pandas
- How to delete a label for a kubernetes pod
- How to delete all resources from Helm list by one command?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.