• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Logging Timestamp – Print Current Time to Console

Python Logging Timestamp – Print Current Time to Console

February 8, 2022 Leave a Comment

In Python, it can be very useful to add a timestamp when logging information to the console. We can log the timestamp to the console with the logging module easily by adjusting the basic configuration.

import logging

logging.basicConfig(
    format='%(asctime)s %(levelname)-8s %(message)s',
    level=logging.INFO,
    datefmt='%Y-%m-%d %H:%M:%S')

logging.info('Message for the user.')

#Output:
2022-01-25 07:58:28 INFO     Message for the user.

The logging module is very useful for developers to add logging calls in their code to print messages to the console when certain messages occur.

Many times, a simple message is not enough, and we want to add time to our logging messages.

We can easily add timestamps to our logging messages by changing the basic configuration of the logging module.

Below is some sample code for you to see how you can use the basicConfig property of the logging module to change the message format and add the timestamp.

import logging

logging.basicConfig(
    format='%(asctime)s %(levelname)-8s %(message)s',
    level=logging.INFO,
    datefmt='%Y-%m-%d %H:%M:%S')

logging.info('Message for the user.')

#Output:
2022-01-25 07:58:28 INFO     Message for the user.

Printing the Current Time To Console Without Logging Module

If you are just trying to print the current time to the console, and you are not using the logging module, you can do so easily in Python with the datetime module.

To get the current timestamp and print it to the console, we use the now() and timestamp() functions from the datetime module.

Below is the code you can use to print the current time to the console using Python.

import datetime
  
current_time = datetime.datetime.now()
timestamp_of_current_time = current_time.timestamp()

print(timestamp_of_current_time)

#Output:
1644352484.462

Hopefully this article has been useful for you to learn how to use the logging module to add the timestamp to your logging messages.

Other Articles You'll Also Like:

  • 1.  Get Random Value from Dictionary in Python
  • 2.  How to Create Array from 1 to n in Python
  • 3.  pandas percentile – Calculate Percentiles of Series or Columns in DataFrame
  • 4.  Check if String Contains Numbers in Python
  • 5.  How to Check if a String Contains Vowels in Python
  • 6.  List of Turtle Shapes in Python
  • 7.  Read Last Line of File Using Python
  • 8.  Using Python to Get Domain from URL
  • 9.  pandas sum – Get Sum of Series or DataFrame Columns
  • 10.  Using Python to Count Number of True in List

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy