user3214546
user3214546

Reputation: 6831

Is there any way in django to include some module in every page

In my django app i have to include logger in almost every page around 30 different files like this

import logging
logger_test = logging.getLogger('test')

logger_test.info('test')

Is there any way that gets included in all files and i can just run

logger_test.info('test')

Upvotes: 1

Views: 70

Answers (1)

kalhartt
kalhartt

Reputation: 4129

First, I would prefer just putting logger_test somewhere and using from myapp import logger_test. One extra line per file is much less likely to wreak havoc than the following.

You can do this using the appropriately named builtins module (py2 __builtins__). Just add the logger to the builtins module anywhere, putting this in settings.py is probably a good choice.

import builtins
import logging

logger = logging.getLogger('test')
setattr(builtins, 'logger_test', logger)

Now every module will have logger_test available. A last warning, this is a truly terrible idea.

Upvotes: 3

Related Questions