Print timestamp for logging in Python
Before the first time you log anything do this:
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
Example on the REPL:
>>> import logging
>>> logging.basicConfig(
... format='%(asctime)s %(levelname)-8s %(message)s',
... level=logging.INFO,
... datefmt='%Y-%m-%d %H:%M:%S')
>>>
>>> logging.info('an info messge')
2017-05-25 00:58:28 INFO an info messge
>>> logging.debug('a debug messag is not shown')
>>>
How to Customize the time format for Python logging?
From the official documentation regarding the Formatter class:
The constructor takes two optional arguments: a message format string and a date format string.
So change
# create formatter
formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s")
to
# create formatter
formatter = logging.Formatter("%(asctime)s;%(levelname)s;%(message)s",
"%Y-%m-%d %H:%M:%S")
Adding timestamps to all console messages
It turns out, you can override the console functions at the top of the app.js file, and have it take effect in every other module. I got mixed results because one of my modules is forked as a child_process
. Once I copied the line to the top of that file as well, all works.
For the record, I installed the module console-stamp (npm install console-stamp --save
), and added this line to the top of app.js and childProcess.js:
// add timestamps in front of log messages
require('console-stamp')(console, '[HH:MM:ss.l]');
My problem now was that the :date
format of the connect logger uses UTC format, rather than the one I'm using in the other console calls. That was easily fixed by registering my own time format (and as a side effect, requiring the dateformat
module that console stamp
comes with, rather than installing another one):
// since logger only returns a UTC version of date, I'm defining my own date format - using an internal module from console-stamp
express.logger.format('mydate', function() {
var df = require('console-stamp/node_modules/dateformat');
return df(new Date(), 'HH:MM:ss.l');
});
app.use(express.logger('[:mydate] :method :url :status :res[content-length] - :remote-addr - :response-time ms'));
Now my log files look organized (and better yet, parseable):
[15:09:47.746] staging server listening on port 3000
[15:09:49.322] connected to database server xxxxx successfully
[15:09:52.743] GET /product 200 - - 127.0.0.1 - 214 ms
[15:09:52.929] GET /stylesheets/bootstrap-cerulean.min.css 304 - - 127.0.0.1 - 8 ms
[15:09:52.935] GET /javascripts/vendor/require.js 304 - - 127.0.0.1 - 3 ms
[15:09:53.085] GET /javascripts/product.js 304 - - 127.0.0.1 - 2 ms
...
Add the time in a log name using logger
I got help from another website.
You have to change the hdlr to:
({FOLDER LOCATION}/Logs/log{}.log'.format(datetime.datetime.strftime(datetime.datetime.now(), '%Y%m%d%H%M%S_%f')))
How do I prefix my log messages with the current date and time?
This worked for me (adapted from thorndeux's answer):
import logging.config
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'prepend_date': {
'format': '{asctime} {levelname}: {message}',
'style': '{',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'prepend_date',
},
},
'root': {
'handlers': ['console'],
'level': 'INFO',
},
}
logging.config.dictConfig(LOGGING)
logging.info('foo')
logging.warning('bar')
prints
2021-11-28 16:05:13,469 INFO: foo
2021-11-28 16:05:13,469 WARNING: bar
How to introduce date and time in log file
You need to look into using date
and either gmtime
or localtime
to get the actual date and time.
Then strftime
can format it for you.
Sample program follows:
#include <stdio.h>
#include <time.h>
int main (void) {
char buff[20];
struct tm *sTm;
time_t now = time (0);
sTm = gmtime (&now);
strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", sTm);
printf ("%s %s\n", buff, "Event occurred now");
return 0;
}
This outputs:
2011-09-14 04:52:11 Event occurred now
I prefer the use of UTC rather than local time since it allows you to tie together events from geographically separated machine without worrying about timezone differences. In other words, use gmtime
rather than localtime
unless you're very certain you won't be crossing timezones.
I also tend to prefer the YYYY-MM-DD HH:MM:SS
format since it's easier to sort than month names, vital for extraction and manipulation tools.
If you have an implementation that provides the optional bounds-checking functions (as per Appendix K of C11), you can probably use gmtime_s
in preference. It allows you to specify your own buffer for receiving the result and is thus safer in re-entrant and/or threaded code.
To use that, you need to change your code to something like:
#include <stdio.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>
int main (void) {
char buff[20];
struct tm sTm;
time_t now = time (0);
gmtime_s (&now, &sTm);
strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", &sTm);
printf ("%s %s\n", buff, "Event occurred now");
return 0;
}
Although you should be aware that the folks at Microsoft have somehow managed to get the arguments for gmtime_s
around the wrong way. You'll need to take that into account.
POSIX (and Linux) also provides a gmtime_r
function which performs in the same way as the standard gmtime_s
function (with the arguments in the correct order).
How do you add datetime to a logfile name?
You need datetime.strftime()
, this allows you to format the timestamp using all of the directives of C's strftime()
. In your specific case:
>>> datetime.now().strftime('mylogfile_%H_%M_%d_%m_%Y.log')
'mylogfile_08_48_04_02_2012.log'
How to Change the time zone in Python logging?
How to log the timezone
%Z
from strftime format
Windows
>>> import logging
>>> logging.basicConfig(format="%(asctime)s %(message)s", datefmt="%m/%d/%Y %I:%M:%S %p %Z")
>>> logging.error('test')
11/03/2017 02:29:54 PM Mountain Daylight Time test
Linux
>>> import logging
>>> logging.basicConfig(format="%(asctime)s %(message)s", datefmt="%m/%d/%Y %I:%M:%S %p %Z")
>>> logging.error('test')
11/03/2017 02:30:50 PM MDT test
If the question is
How do I log in a different timezone than the local time on the server?
part of the answer is logging.Formatter.converter, however, you have to understand naive and aware datetime objects. Unless you want to write your own timezone module, I highly suggest the pytz library (pip install pytz
). Python 3 includes a UTC and UTC offset timezone, but there's rules you'll have to implement for daylight saving or other offsets, so I would suggest the pytz library, even for python 3.
For example,
>>> import datetime
>>> utc_now = datetime.datetime.utcnow()
>>> utc_now.isoformat()
'2019-05-21T02:30:09.422638'
>>> utc_now.tzinfo
(None)
If I apply a timezone to this datetime object, the time won't change (or will issue a ValueError
for < python 3.7ish).
>>> mst_now = utc_now.astimezone(pytz.timezone('America/Denver'))
>>> mst_now.isoformat()
'2019-05-21T02:30:09.422638-06:00'
>>> utc_now.isoformat()
'2019-05-21T02:30:09.422638'
However, if instead, I do
>>> import pytz
>>> utc_now = datetime.datetime.now(tz=pytz.timezone('UTC'))
>>> utc_now.tzinfo
<UTC>
now we can create a properly translated datetime
object in whatever timezone we wish
>>> mst_now = utc_now.astimezone(pytz.timezone('America/Denver'))
>>> mst_now.isoformat()
'2019-05-20T20:31:44.913939-06:00'
Aha! Now to apply this to the logging module.
Epoch timestamp to string representation with timezone
The LogRecord.created
attribute is set to the time when the LogRecord
was created (as returned by time.time()
), from the time
module. This returns a timestamp (seconds since the epoch). You can do your own translation to a given timezone, but again, I suggest pytz
, by overriding the converter.
import datetime
import logging
import pytz
class Formatter(logging.Formatter):
"""override logging.Formatter to use an aware datetime object"""
def converter(self, timestamp):
dt = datetime.datetime.fromtimestamp(timestamp)
tzinfo = pytz.timezone('America/Denver')
return tzinfo.localize(dt)
def formatTime(self, record, datefmt=None):
dt = self.converter(record.created)
if datefmt:
s = dt.strftime(datefmt)
else:
try:
s = dt.isoformat(timespec='milliseconds')
except TypeError:
s = dt.isoformat()
return s
Python 3.5, 2.7
>>> logger = logging.root
>>> handler = logging.StreamHandler()
>>> handler.setFormatter(Formatter("%(asctime)s %(message)s"))
>>> logger.addHandler(handler)
>>> logger.setLevel(logging.DEBUG)
>>> logger.debug('test')
2019-05-20T22:25:10.758782-06:00 test
Python 3.7
>>> logger = logging.root
>>> handler = logging.StreamHandler()
>>> handler.setFormatter(Formatter("%(asctime)s %(message)s"))
>>> logger.addHandler(handler)
>>> logger.setLevel(logging.DEBUG)
>>> logger.debug('test')
2019-05-20T22:29:21.678-06:00 test
Substitute America/Denver
with America/Anchorage
for the posix timezone as defined by pytz
>>> next(_ for _ in pytz.common_timezones if 'Alaska' in _)
'US/Alaska'
US/Alaska is deprecated
>>> [_ for _ in pytz.all_timezones if 'Anchorage' in _]
['America/Anchorage']
Local
If you got to this question and answers looking for how to log the local timezone, then instead of hardcoding the timezone, get tzlocal
(pip install tzlocal
) and replace
tzinfo = pytz.timezone('America/Denver')
with
tzinfo = tzlocal.get_localzone()
Now it will work on whatever server runs the script, with the timezone on the server.
Caveat when not logging UTC
I should add, depending on the application, logging in local time zones can create ambiguity or at least confusion twice a year, where 2 AM is skipped or 1 AM repeats, and possibly others.
Node.js - How to add timestamp to logs using Winston library?
I was dealing with the same issue myself. There are two ways I was able to do this.
When you include Winston, it usually defaults to adding a Console transport. In order to get timestamps to work in this default case, I needed to either:
- Remove the console transport and add again with the timestamp option.
- Create your own Logger object with the timestamp option set to true.
The first:
var winston = require('winston');
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, {'timestamp':true});
The second, and cleaner option:
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({'timestamp':true})
]
});
Some of the other options for Console transport can be found here:
- level: Level of messages that this transport should log (default 'debug').
- silent: Boolean flag indicating whether to suppress output (default false).
- colorize: Boolean flag indicating if we should colorize output (default false).
- timestamp: Boolean flag indicating if we should prepend output with timestamps (default false). If function is specified, its return value will be used instead of timestamps.
Related Topics
Trying to Get Svn2Git Working on Windows
How to Change Case of Letters in String Using Regex in Ruby
Append Row to CSV File Ruby 1.9 CSV Lib
Naming Conventions for Boolean Attributes
Get Available Diskspace in Ruby
How to Create a Folder (If Not Present) with Logger.New
How to Bundle Install Gemfile with Specific Version of Bundler
How to Get Class-Object from String "A::B::C" in Ruby
How to Export a Rails Model to JSON Schema
Emacs Ruby Method Parameter Indentation
How to Replace an Array's Element
How to Find Out the Starting Point for Each Match in Ruby
Prawnto Displaying Tables That Don't Break When New Page
Error While Installing Ruby 1.9.3
Is There a Shorthand If (Without Else) Statement in Ruby on Rails