rodfersou
(Rodrigo Ferreira de Souza)
August 23, 2017, 4:49pm
1
Hey Folks,
We are rewriting some scripts to stop using clock server and start using crontab.
To be simple, I wrote a simple script to call the already existent view with crontab:
# -*- coding: utf-8 -*-
"""scripts/call_view.py"""
from plone import api
import sys
import transaction
portal = api.portal.get()
view_name = sys.argv[-1]
api.content.get_view(name=view_name, context=portal, request=app.REQUEST)()
transaction.commit()
And add the call of this script at our buildout with crontab:
#example_buildout.cfg
[buildout]
parts +=
process-analytics
process-feeds
process-cma
[process-analytics]
recipe = z3c.recipe.usercrontab
times = 0 */2 * * *
command = ${buildout:bin-directory}/instance -O Plone run ${buildout:directory}/scripts/call-view.py process-analytics
[process-feeds]
recipe = z3c.recipe.usercrontab
times = 20 * * * *
command = ${buildout:bin-directory}/instance -O Plone run ${buildout:directory}/scripts/call-view.py process-all-feeds
[process-cma]
recipe = z3c.recipe.usercrontab
times = 40 */4 * * *
command = ${buildout:bin-directory}/instance -O Plone run ${buildout:directory}/scripts/call-view.py process-cma
Like this scripts are running fine, but the logger inside the views are not.
Can someone guide me on how to make logger work again?
1 Like
Hi man!
Does not it just work with "import logging" or like here https://docs.plone.org/4/en/develop/plone/misc/commandline.html#spoofing-http-request ?
# -*- coding: utf-8 -*-
"""scripts/call_view.py"""
from plone import api
import sys, **logging**
import transaction
**logger = logging.getLogger('My Magic Script')**
portal = api.portal.get()
view_name = sys.argv[-1]
api.content.get_view(name=view_name, context=portal, request=app.REQUEST)()
**logger.info('Yes we can!')**
transaction.commit()
zopyx
(Andreas Jung)
August 24, 2017, 3:29am
3
If you are looking for persistent per-object logging then check out zopyx.plone.persistenlogger.
rodfersou
(Rodrigo Ferreira de Souza)
August 24, 2017, 5:13am
4
Thanks for the answer!
I tried something like what you said, but the messages is not getting into var/log/instance.log when I use instance run
Also, my view already has the logger, it worked fine with clock server, so my script don't need to add a new logger.
It is just not working when I execute this way for some reason..
rodfersou
(Rodrigo Ferreira de Souza)
August 24, 2017, 5:15am
5
Thank you for the link, but that's not what I need..
erral
(Mikel Larreategi)
August 24, 2017, 10:55am
6
We had a similar issue and ended creating a specific logger in the script
that logged to a specific log file instead of to instance.log
rodfersou
(Rodrigo Ferreira de Souza)
August 24, 2017, 1:17pm
8
nice @erral ! I tried something like this yesterday without success.. is it possible to share this code?
idgserpro
(Idgserpro)
August 24, 2017, 2:00pm
9
hvelarde
(hvelarde)
August 24, 2017, 2:03pm
10
The real question here is: does anybody knows why the instance logger doesn't work when using the "run" command?
As @rodfersou mentioned we are using code with logger.info()
calls that normally are dumped to an instance.log
file then the instance is running using instance start
. But those same calls don't log anything when the code is called using instance run
.
I made a test and I noticed that only WARN
level is shown in the display even when have configured the access log at INFO
level.
Is there a way to change this behavior?
3 Likes
idgserpro
(Idgserpro)
August 24, 2017, 2:44pm
11
We agree with you, the suggestion was a last case scenario. Don't know if it's really necessary, but did you try to spoof the REQUEST and see if it works? Or add a new handler to the log like it's shown in the spoof REQUEST
code snippet?
erral
(Mikel Larreategi)
August 25, 2017, 8:20pm
12
It's something like this
import logging
import sys
import os
# Force application logging level to INFO and log output to stdout for all loggers
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
handler.setFormatter(formatter)
root_logger.addHandler(handler)
dir_path = os.path.dirname(os.path.realpath(os.path.realpath(sys.argv[-1])))
fh = logging.FileHandler('{}/var/log/directory_full_import.log'.format(dir_path))
fh.setLevel(logging.INFO)
fh.setFormatter(formatter)
root_logger.addHandler(fh)
from logging import getLogger
log = getLogger('directory_full_import')
1 Like
rodfersou
(Rodrigo Ferreira de Souza)
August 25, 2017, 8:36pm
13
OMG.. too much code for something that should just work in first place..
Can someone point out if is it possible to fix in the core?
icemac
(Michael Howitz)
August 29, 2017, 9:36am
14
We always use messages on the WARN level in run scripts - even so this is annoying. We had a look into the code but there seems no easy way to change this behaviour. A ticket in the Zope issue tracker would be nice.
1 Like
hvelarde
(hvelarde)
August 29, 2017, 2:41pm
15
1 Like