A Moya web application can generate a number of log messages, useful for monitoring what your site is doing and diagnosing errors. Moya's logging system is highly configurable and allows you to set which log messages you see and where they are written.
Moya borrows its logging system from Python's logging library and has much the same capabilities.
Log messages are emited from various loggers. A logger has a name which identifies the source of the log messages. Moya uses the name moya
for its log messages. Loggers are further categorized with a dot. The following lists the loggers used by Moya:
moya.app.auth
would be the name used for log messages written by the application named "auth".
Each log messages has a log level, which defines the severity of the log message. Log messages may be filtered based on the log level, so you only see the type of message you are interested in. The log levels are as follows:
You can write log messages of your own with the <log> tag. The message goes in the enclosed text. Here's an example:
<log>Your log messages goes here</log>
You may set the logging level with the level
attribute. For example:
<log level="debug">1s and 0s</log><log level="info">Thought you might want to know</log><log level="error">I've made a terrible mistake</log>
For each of the logging levels there is a log tag that write logs just that level. For instance, <log-debug> writes a debug log and <log-warn> writes a warning log. These level specific log tags are identical to <log> in all other respects.
The logging system may be configured via an INI file, which allows you to set which messages are to be filtered, and where they should will be written (e.g. to the console or to a file).
The development server uses logging configuration taken from a file called logging.ini
which is set up to write all message to the console. In production, Moya uses the file prodlogging.ini
which shows only messages with level info or higher.
You can change which logging configuration file is used in the development server with the --logging
switch. This is useful for checking your production logging settings. For example:
$ moya --logging prodlogging.ini runserver
The logging configuration file used in production is set in the WSGI file (see Deploying). The default prodlogging.ini
writes log messages with a level of INFO or higher to syslog. Note that the syslog configuration assumes the defaults for most Linux Distros, if you are running on OSX or Windows, you will need enable the configuration for your platform which will be commented out in prodlogging.ini
.
Moya's logging INI format is similar to the configuration format used by Python's logging module, but is more consistent with Moya's other INI files (and somewhat less prone to errors).
You can configure a logger with a named section as follows:
[logger:moya]level = DEBUG
This sets a level of DEBUG on the moya
logger, which ensures that all log messages are written.
Note that when a log message is written, the logging system looks for all handlers in the hierarchy defined with the dotted notation. For example if the moya.app.auth
logger emits a message, it will be written to moya.app.auth
, moya.app
, moya
, and finally a special logger called root
. If you don't want this behavior, you can set propagate=no
on the logger, to stop writing to the loggers further up the chain.
A formatter sets how a log message is formatted. Here's the default formatter that writes the log message along with the date and level:
[formatter:simple]format = %(asctime)s:%(name)s:%(levelname)s: %(message)sdatefmt = [%d/%b/%Y %H:%M:%S]
A handler sets the destination for logging messages (file, console, syslog etc). See the Python documentation for the full details on creating formatters. Otherwise, here are the default handlers used by Moya:
# Writes syntax highlighted log messages to the terminal[handler:moyaconsole]class = moya.logtools.MoyaConsoleHandlerformatter = simpleargs = (sys.stdout,)# Writes simple text to the terminal[handler:stdout]class = StreamHandlerformatter = simpleargs = (sys.stdout,)# Writes to a syslog server[handler:syslog]class = logging.handlers.SysLogHandlerargs = ('/dev/log',)
You can set which handler a logger should use with the handler
key. For example:
[logger:moya]level = DEBUGhandler = moyaconsole
Other loggers not under the moya
namespace may be configured with Moya's logging configuration. The sqlalchemy
logger is of particular interest, as it can log all the SQL queries going to the database. If you add the following to your logging ini, you will see SQL related information in the terminal:
[logger:sqlalchemy.engine]handlers = moyaconsolepropagate = nolevel = INFO
You can set the level
to DEBUG to display more details, although the messages there are less useful than the SQL. Set level
to WARN to disable sqlalchemy logs.