展开

文章目录

修改历史

web2py的一些不常用的配置记录

2021-11-12 10:25:50 Python 41

简介

web2py是python中的一个web开发框架,由于其在python web圈子中地位相较于Django、Flask比较低,很多问题中文难以搜索得到,Google也很比较难搜索,因此记录一下在日常工作中遇到的一些问题。

1. 错误日志配置

web2py的根目录中有一个examples的文件夹,其中有一个文件名为logging.example.conf的文件,执行下面的命令将其拷贝到web2py的根目录中。

cp logging.example.conf ../logging.conf

logging.conf文件的内容如下所示

#  Configure the Python logging facility.
#  To use this file, copy it to logging.conf and edit logging.conf as required.
#  See http://docs.python.org/library/logging.html for details of the logging facility.
#  Note that this is not the newer logging.config facility.
#
#  The default configuration is console-based (stdout) for backward compatibility;
#  edit the [handlers] section to choose a different logging destination.
#
#  Note that file-based handlers are thread-safe but not mp-safe;
#  for mp-safe logging, configure the appropriate syslog handler.
#
#  To create a configurable logger for application 'myapp', add myapp to
#  the [loggers] keys list and add a [logger_myapp] section, using
#  [logger_welcome] as a starting point.
#
#  In your application, create your logger in your model or in a controller:
#
#  import logging
#  logger = logging.getLogger("web2py.app.myapp")
#  logger.setLevel(logging.DEBUG)
#
#  To log a message:
#
#  logger.debug("You ought to know that %s", details)
#
#  Note that a logging call will be governed by the most restrictive level
#  set by the setLevel call, the [logger_myapp] section, and the [handler_...]
#  section. For example, you will not see DEBUG messages unless all three are
#  set to DEBUG.
#
#  Available levels: DEBUG INFO WARNING ERROR CRITICAL

[loggers]
keys=root,rocket,markdown,web2py,rewrite,cron,app,welcome

[handlers]
keys=consoleHandler,messageBoxHandler,rotatingFileHandler
#keys=consoleHandler,rotatingFileHandler
#keys=osxSysLogHandler
#keys=notifySendHandler

[formatters]
keys=simpleFormatter

[logger_root]
level=WARNING
handlers=consoleHandler,rotatingFileHandler

[logger_web2py]
level=WARNING
handlers=consoleHandler,rotatingFileHandler
qualname=web2py
propagate=0

#  URL rewrite logging (routes.py)
#  See also the logging parameter in routes.py
#
[logger_rewrite]
level=WARNING
qualname=web2py.rewrite
handlers=consoleHandler,rotatingFileHandler
propagate=0

[logger_cron]
level=WARNING
qualname=web2py.cron
handlers=consoleHandler,rotatingFileHandler
propagate=0

# generic app handler
[logger_app]
level=WARNING
qualname=web2py.app
handlers=consoleHandler,rotatingFileHandler
propagate=0

# welcome app handler
[logger_welcome]
level=WARNING
qualname=web2py.app.welcome
handlers=consoleHandler,rotatingFileHandler
propagate=0

# loggers for legacy getLogger calls: Rocket and markdown
[logger_rocket]
level=WARNING
handlers=consoleHandler,messageBoxHandler,rotatingFileHandler
qualname=Rocket
propagate=0

[logger_markdown]
level=WARNING
handlers=consoleHandler,rotatingFileHandler
qualname=markdown
propagate=0

[handler_consoleHandler]
class=StreamHandler
level=WARNING
formatter=simpleFormatter
args=(sys.stdout,)

[handler_messageBoxHandler]
class=gluon.messageboxhandler.MessageBoxHandler
level=ERROR
formatter=simpleFormatter
args=()

[handler_notifySendHandler]
class=gluon.messageboxhandler.NotifySendHandler
level=ERROR
formatter=simpleFormatter
args=()

# Rotating file handler
#   mkdir logs in the web2py base directory if not already present
#   args: (filename[, mode[, maxBytes[, backupCount[, encoding[, delay]]]]])
#
[handler_rotatingFileHandler]
class=handlers.RotatingFileHandler
level=DEBUG
formatter=simpleFormatter
args=("web2py.log", "a", 1000000, 5)

[handler_osxSysLogHandler]
class=handlers.SysLogHandler
level=WARNING
formatter=simpleFormatter
args=("/var/run/syslog", handlers.SysLogHandler.LOG_DAEMON)

[handler_linuxSysLogHandler]
class=handlers.SysLogHandler
level=WARNING
formatter=simpleFormatter
args=("/dev/log", handlers.SysLogHandler.LOG_DAEMON)

[handler_remoteSysLogHandler]
class=handlers.SysLogHandler
level=WARNING
formatter=simpleFormatter
args=(('sysloghost.domain.com', handlers.SYSLOG_UDP_PORT), handlers.SysLogHandler.LOG_DAEMON)

[formatter_simpleFormatter]
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
datefmt=

假设你的application名称为hello,在logging.conf文件中添加如下内容,为hello应用开启logging日志记录

[logger_hello]
level=WARNING
qualname=web2py.app.hello
handlers=consoleHandler,rotatingFileHandler
propagate=0

同时在你的models目录的第一个文件中添加下面的代码

import logging
logger = logging.getLogger('web2py.app.hello')

这样你在你的controller中就可以使用如下代码来追踪错误日志了

# hello.py

def index():
    try:
        user = db(db.auth_user).select()
    except:
        import traceback
        logger.error(traceback.format_exc())

2. 配置默认应用

web2pyapplications目录中默认的会有welcome这个应用,当我们启动服务访问http://127.0.0.1:8000的时候会自动定位到welcome这个application,其实我们可以通过配置来自行修改这个默认应用。进入到web2py的根目录中,新建一个routes.py文件

cd /var/web2py
vim routes.py

假设我们需要默认定位的应用为hello,在routes.py文件中输入下面的内容

default_application = "hello"
default_controller = "default"
default_function = "index"

如果你想默认不是跳转到首页,你也可以自行定义default_controller以及default_function两个参数。个人认为这个配置一般的是在部署到服务器的时候使用域名访问的时候比较有用,在本地开发测试的时候没有必要去配置。

 

 

当前共有0条评论