Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

CHAPTER

EIGHTEEN

LOGGING

Pyramid allows you to make use of the Python standard library logging module. This chapter describes how to configure logging and how to send log messages to loggers that you’ve configured.

latex-warning.png

This chapter assumes you’ve used a scaffold to create a project which contains development.ini and production.ini files which help configure logging. All of the scaffolds which ship along with Pyramid do this. If you’re not using a scaffold, or if you’ve used a third-party scaffold which does not create these files, the configuration information in this chapter will not be applicable.

18.1 Logging Configuration

A Pyramid project created from a scaffold is configured to allow you to send messages to Python standard library logging package loggers from within your application. In particular, the PasteDeploy development.ini and production.ini files created when you use a scaffold include a basic configuration for the Python logging package.

PasteDeploy .ini files use the Python standard library ConfigParser format; this the same format used as the Python logging module’s Configuration file format. The application-related and logging-related

197

18. LOGGING

sections in the configuration file can coexist peacefully, and the logging-related sections in the file are used from when you run pserve.

The pserve command calls the logging.fileConfig function using the specified ini file if it contains a [loggers] section (all of the scaffold-generated .ini files do). logging.fileConfig reads the logging configuration from the ini file upon which pserve was invoked.

Default logging configuration is provided in both the default development.ini and the production.ini file. The logging configuration in the development.ini file is as follows:

1

# Begin logging configuration

2

 

3

[loggers]

4

keys = root, {{package_logger}}

5

 

6

[handlers]

7

keys = console

8

 

9

[formatters]

10

keys = generic

11

 

12[logger_root]

13level = INFO

14handlers = console

15

16[logger_{{package_logger}}]

17level = DEBUG

18handlers =

19qualname = {{package}}

20

21[handler_console]

22class = StreamHandler

23args = (sys.stderr,)

24level = NOTSET

25formatter = generic

26

27[formatter_generic]

28format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s

29

30 # End logging configuration

The production.ini file uses the WARN level in its logger configuration, but it is otherwise identical.

The name {{package_logger}} above will be replaced with the name of your project’s package, which is derived from the name you provide to your project. For instance, if you do:

198

18.1. LOGGING CONFIGURATION

1 pcreate -s starter MyApp

The logging configuration will literally be:

1 # Begin logging configuration

2

3 [loggers]

4

keys

=

root, myapp

5

 

 

 

6

[handlers]

7

keys

=

console

8

 

 

 

9 [formatters]

10 keys = generic

11

12[logger_root]

13level = INFO

14handlers = console

15

16[logger_myapp]

17level = DEBUG

18handlers =

19qualname = myapp

20

21[handler_console]

22class = StreamHandler

23args = (sys.stderr,)

24level = NOTSET

25formatter = generic

26

27[formatter_generic]

28format = %(asctime)s %(levelname)-5.5s [%(name)s][%(threadName)s] %(message)s

29

30 # End logging configuration

In this logging configuration:

a logger named root is created that logs messages at a level above or equal to the INFO level to stderr, with the following format:

2007-08-17 15:04:08,704 INFO [packagename]

Loading resource, id: 86

199

18. LOGGING

a logger named myapp is configured that logs messages sent at a level above or equal to DEBUG to stderr in the same format as the root logger.

The root logger will be used by all applications in the Pyramid process that ask for a logger (via logging.getLogger) that has a name which begins with anything except your project’s package name (e.g. myapp). The logger with the same name as your package name is reserved for your own usage in your Pyramid application. Its existence means that you can log to a known logging location from any Pyramid application generated via a scaffold.

Pyramid and many other libraries (such as Beaker, SQLAlchemy, Paste) log a number of messages to the root logger for debugging purposes. Switching the root logger level to DEBUG reveals them:

[logger_root]

#level = INFO level = DEBUG handlers = console

Some scaffolds configure additional loggers for additional subsystems they use (such as SQLALchemy). Take a look at the production.ini and development.ini files rendered when you create a project from a scaffold.

18.2 Sending Logging Messages

Python’s special __name__ variable refers to the current module’s fully qualified name. From any module in a package named myapp, the __name__ builtin variable will always be something like myapp, or myapp.subpackage or myapp.package.subpackage if your project is named myapp. Sending a message to this logger will send it to the myapp logger.

To log messages to the package-specific logger configured in your .ini file, simply create a logger object using the __name__ builtin and call methods on it.

1import logging

2log = logging.getLogger(__name__)

3

4def myview(request):

5 content_type = ’text/plain’

6content = ’Hello World!’

7 log.debug(’Returning: %s (content-type: %s)’, content, content_type) 8 request.response.content_type = content_type

9return request.response

This will result in the following printed to the console, on stderr:

200

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]