[英]Running a logger works fine in local project but fails on Heroku
Running a module called log.py
creates a file my_file.log
where it logs at what time the script ran. 运行名为log.py
的模块将创建文件my_file.log
,并在该文件中记录脚本运行的时间。 Running log.py
(over)writes to a log file perfectly fine in my local project but for some reason running it on my production deploy in heroku doesn't write to my_file.log
. 在本地项目中运行log.py
(过度)会写入一个日志文件,但由于某种原因,在heroku中的生产部署中运行它不会写入my_file.log
。 The log.py
also does some other things and I can tell by that that log.py
did indeed execute in Heroku. log.py
还执行其他一些操作,我可以看出log.py
确实在Heroku中执行了。 It's only the logs that aren't written. 只是没有写的日志。
First I thought it could be a permissions problem but checking with ls -l
gives 首先,我认为这可能是权限问题,但使用ls -l
检查可以
drwx------ 3 my_app
-rw------- 1 my_file.log
The following is my project structure: 以下是我的项目结构:
.
├── my_project
│ ├── my_app
│ │ ├── __init__.py
│ │ ├── models.py
│ │ ├── tests.py
│ │ ├── views.py
│ │ ├── my_file.log
│ │ └── management
│ │ ├── __init__.py
│ │ └── commands
│ │ ├── __init__.py
│ │ └── log.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
└── requirements.txt
It shouldn't have anything to do with the code being used as a custom Django command because in my development version the log file is overwritten each time as it should. 它与用作自定义Django命令的代码没有任何关系,因为在我的开发版本中,日志文件每次都应被覆盖。
This is the snippet in log.py
responsible for logging to my_file.log
: 这是log.py
的片段,用于记录到my_file.log
:
log_lc = os.path.join(os.path.dirname(os.path.abspath(__file__)),
os.path.pardir, os.path.pardir, "my_file.log")
logging.basicConfig(filename=log_lc,level=logging.INFO,
format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p',
filemode="w")
logging.info("You just ran my_file.log")
Heroku is read-only. Heroku是只读的。 You are trying to create your own log file, my_file.log
, and you will not be able to do that since you cannot write anything to their file system. 您正在尝试创建自己的日志文件my_file.log
,但无法执行此操作,因为您无法将任何内容写入其文件系统。
You can, however, print log messages to Heroku's standard logs. 但是,您可以将日志消息打印到Heroku的标准日志中。 Here are the docs for that. 这是该文档。 . 。 You could then check your messages via heroku logs
. 然后,您可以通过heroku logs
检查消息。
In addition to Heroku's standard logging, they have many apps available for logging that you can install on your Heroku app. 除了Heroku的标准日志记录之外,它们还有许多可用于记录日志的应用程序,您可以将其安装在Heroku应用程序上。 Otherwise, you could consider using an email option or writing your log file to a third party location like s3
. 否则,您可以考虑使用电子邮件选项或将日志文件写入第三方位置(如s3
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.