How to make a log system for tornado - python

I've read the documentation: http://www.tornadoweb.org/en/stable/log.html
But I still don't know how to make a suitable log for my server, which is built with tornado.
For now, I need such a log system:
It can log everything with time format, and for each day it create a new log file.
It seems that TimedRotatingFileHandler is what I need but I don't know how to use it with tornado.

The Tornado logging streams are just standard loggers from the "logging" python module.
There is nice tutorial on the python website https://docs.python.org/3/howto/logging.html#advanced-logging-tutorial
As per how to set the handler (same tutorial)
https://docs.python.org/3/howto/logging.html#handlers

Related

How to deactivate unwanted Twisted log output when using Scrapyd?

When using print method I am receiving log output I haven't seen before.
I guess it's coming from Twisted module which seems to be a part of Scrapyd. I am not using this kind of logging elsewhere and I haven't started it explicitely.
Does anyone know how to supress/deactivate this kind of log messages?

generating errors and access logs in python

We are realizing that we need error logs and access logs for our service processes. These are long running process like services; they respond to calls made to them.
Hence, we need your help to simply achieve the following:
# for developers
from MyLogger import log
log.error 'something bad something wrong'
log.access 'something something'
I am thinking of designing this MyLogger which will simply redirect an error to stderr and access to stdout, so that I can collect errors to a specific file through configuration for both stderr and stdout.
One more point: these services are nothing but web.py instances.
I guess I'm not looking for a controlling log at various levels, like warn, debug, error, info, etc. My aim is more to have an error log and access log similar to the apache web server. So my developers should not be concerned about using warn, debug, etc. as follows:
log.warn msg
log.debug msg
This is not required.
I just want to have an error log and an access log similar to that of a web server or service.
That "battery" is already included for you in the Python standard logging module.
Python ships with the logging module.

Logging in Python?

I have used log4J and log4N on previous not-pythonic projects. I like heirachy of warnings,errors and escalations. The ability to log the error and if it is serious email the support team. Also automatic log file cycling is important as the it will be running on a small LINUX device.
Can I do this with the standard Python logging module or is there a better approach?
Yes, the logging module has log levels DEBUG, INFO, WARNING, ERROR and CRITICAL. You can setup a SMTPHandler to send mail when the logging level is, say, CRITICAL, and you can setup a RotatingFileHandler to limit the number and size of the log files.
The standard Python logging module is explicitly inspired by log4J, so you will almost certainly find it suitable. It has the same hierarchy, and you can define handlers that listen to one or more levels and do something appropriate, whether it's log to a file or to an email address via SMTP. See the Python logging tutorial.

Capture google app engine logging output

How can one view the Google App Engine logs outside the Admin console?
I'm developing, so using dev_appserver.py/the Admin Console and would like to see the logs as the records are emitted.
I'd like to monitor the logging output in a console with standard Unix tools e.g. less/grep/etc, but there doesn't seem to be an option to direct the logging from the dev_appserver.py command, and I can't open a new file in GAE (e.g. a FileHandler), so file handlers won't work, and I think using a socket/udp handler would be a bit of overkill (if it's even possible).
I'm hopeful there are other options to view the log.
Thanks for reading.
The default logger sends logging output to stderr. Use your shell's method of redirecting stderr to a file (in tcsh, (dev_appserver.py > /dev/tty) >& your_logfile.txt, your shell may vary.)
You can also use the logging module in python to change the logger to send directly to a file if you detect it's running locally (os.environ['SERVER_SOFTWARE'].startswith('Dev'))
You can download the logs using the request_logs parameter of appcfg.py
http://code.google.com/appengine/docs/python/tools/uploadinganapp.html#Downloading_Logs
Edit:
This person came up with a way to send logs over XMPP. His solution is for GAE Java, but this could be adapted to python.
http://www.professionalintellectualdevelopment.com/
http://code.google.com/p/gae-xmpp-logger/

In Django, how do I allow print statements to work with Apache WSGI?

"print" only works in development server.
But what if I want it to work in Apache? Just in case I forget to comment it out...I want to be able to go smoothly without causing errors.
(Just print to nothing)
As for quick print, just can just use:
print >>sys.stderr, 'log msg'
-- then it lands in error.log, of course.
See Graham Dumpleton's post:
WSGI and printing to standard output
What you're proposing is a Bad Idea, but if you insist on doing it anyways, check out the mod_wsgi configuration directives:
WSGIRestrictStdout
Description: Enable restrictions on use of STDOUT.
Syntax: WSGIRestrictStdout On|Off
Default: WSGIRestrictStdout On
Context: server config
Module: mod_wsgi.c
A well behaved Python WSGI application
should never attempt to write any data
directly to sys.stdout or use the
print statement without directing it
to an alternate file object. This is
because ways of hosting WSGI
applications such as CGI use standard
output as the mechanism for sending
the content of a response back to the
web server. If a WSGI application were
to directly write to sys.stdout it
could interfere with the operation of
the WSGI adapter and result in
corruption of the output stream.
In the interests of promoting
portability of WSGI applications,
mod_wsgi restricts access to
sys.stdout and will raise an exception
if an attempt is made to use
sys.stdout explicitly.
The only time that one might want to
remove this restriction is purely out
of convencience of being able to use
the print statement during debugging
of an application, or if some third
party module or WSGI application was
errornously using print when it
shouldn't. If restrictions on using
sys.stdout are removed, any data
written to it will instead be sent
through to sys.stderr and will appear
in the Apache error log file.
If you want to write print statements to Apache error log, you can use sys.stderr:
import sys
sys.stderr.write('log mgs')
then it will be in apache error log file.

Categories

Resources