librelist archives

« back to archive

no logging when flask deploy with nginx+uwsgi

no logging when flask deploy with nginx+uwsgi

From:
linnchord
Date:
2011-08-10 @ 15:12
hi, 

When run my app alone by 'python appname.py', it's all OK. But when I run 
by uwsgi, my logger can only write once after uwsgi start, after that it 
write nothing an no error no exception.

simple code:

@app.route('/test')
def test():
 logger.error('---test in test!') #When uwsgi start and I first check this
url, it can write one log line. But when I do it again, Nothing, no error 
no exception.
 print('---test in test!') #This is always OK. printed every time.
 return render_template('test.html',data='hello world!')


my origin code:

@app.errorhandler(500)
def error_server(e):
 errorMsg = str(e)
 return helper.render_error(errorMsg, request, 500)


def render_error(errorMsg, req, http_status):

 logger.error('---test in render_error!') #Nothing logged
 logger.error(u'%s-%s: %s %s %s' % (request.remote_addr,
http_status,
errorMsg,
str(req).encode('utf-8'),
str(req.form).encode('utf-8'))) #Nothing logged
print('---test in render_error!')# This is always OK.
 return render_template('json_server_error.html',errorCode=http_status, 
errorMsg=errorMsg), http_status #json format error to client. This is 
always OK.



[helper.render_error] will call 'logger.error' and render error to html. 
When start uwsgi, the logic of business is OK, the error render to client 
is also OK, but the logger is nothing logged.

others: 

uwsgi run by vhost and nginx conf below

server {
 listen 80;
charset utf-8;
 server_name site.com;
 root /var/www/site;

 location /static/ {
 root /var/www/site/static/;
 }

location /favicon.ico {
 log_not_found off;
 alias /var/www/site/favicon.ico;
 }

 location ~*.(jpg|jpeg|3gpp|mp3|gif|png|ico|css|js)$ {
 expires 60d;
 add_header Cache-Control public;
 }

location /nginx_status {
 stub_status on;
 access_log  off;
 }

 location / {
 include uwsgi_params;
 uwsgi_param UWSGI_PYHOME /usr;
 uwsgi_pass 127.0.0.1:9100;
 uwsgi_param UWSGI_CHDIR /var/www/site;
 uwsgi_param UWSGI_SCRIPT site:app;
 uwsgi_param SERVER_NAME site;
 }
}



I'm tired to google, a long time, nothing find. And I'm new to Python, so ...

Any thoughts here?

Thanks.

-------------------------------
Linnchord Gao

linnchord@gmail.com

Re: [flask] no logging when flask deploy with nginx+uwsgi

From:
Armin Ronacher
Date:
2011-08-10 @ 15:23
Hi,

On 8/10/11 5:12 PM, linnchord wrote:
> When run my app alone by 'python appname.py', it's all OK. But when I
> run by uwsgi, my logger can only write once after uwsgi start, after
> that it write nothing an no error no exception.
Logging is disabled by default if not run in debug mode.  Here is how
you can configure it:

http://flask.pocoo.org/docs/errorhandling/


Regards,
Armin

回复: [flask] no logging when flask deploy with nginx+uwsgi

From:
linnchord
Date:
2011-08-10 @ 15:51
thanks, but not this.  

DEBUG=false is the first line of my app config.

I don't understand, what's the diffrent between two situation. Base wsgi 
protocol, it is not the same?  

-------------------------------
linnchord@gmail.com

已使用 Sparrow (http://www.sparrowmailapp.com/?sig) 发送  

在 2011年8月10日星期三,下午11:23,Armin Ronacher 写道:

> Hi,
>  
> On 8/10/11 5:12 PM, linnchord wrote:
> > When run my app alone by 'python appname.py', it's all OK. But when I
> > run by uwsgi, my logger can only write once after uwsgi start, after
> > that it write nothing an no error no exception.
> Logging is disabled by default if not run in debug mode. Here is how
> you can configure it:
>  
> http://flask.pocoo.org/docs/errorhandling/
>  
>  
> Regards,
> Armin

Re: 回复: [flask] no logging when flas

From:
Armin Ronacher
Date:
2011-08-10 @ 15:54
On 2011-08-10 5:51 PM, linnchord wrote:
> thanks, but not this.
>
> DEBUG=false is the first line of my app config.
Which causes the logging to be disabled.  In non-debug mode you have to 
configure the logger by hand.


Regards,
Armin

回复: [flask] no logging when flas

From:
linnchord
Date:
2011-08-10 @ 16:23
hi

I know it.

There's a logger.conf in app root

-----------------------------------------------------------------------------------------------
[loggers]
keys=root,myapp


[handlers]
keys=console,timedRotating


[formatters]
keys=baseFormatter


[logger_root]
level=DEBUG
handlers=console


[logger_myapp]
handlers=console,timedRotating
qualname=myapp
propagate=0


[handler_console]
class=StreamHandler
level=DEBUG
formatter=baseFormatter
args=(sys.stdout,)


[handler_timedRotating]
class=handlers.TimedRotatingFileHandler
level=WARNING
formatter=baseFormatter
args=('log/myapp.log','d',1,15)


[formatter_baseFormatter]
format=%(asctime)s - %(levelname)s: %(message)s




and a logger.py
-------------------------------------------------------
import logging
import logging.config

logging.config.fileConfig('logger.conf')
logger = logging.getLogger('myapp')




a app config config/production.py
-----------------------------------------
DEBUG=False
…


load app config in myapp.py
-----------------------------------------
from logger import logger

app = Flask(__name__)
app.config.from_object('config.production')



ok, is there something conflict?


-------------------------------
linnchord@gmail.com

已使用 Sparrow (http://www.sparrowmailapp.com/?sig) 发送  

在 2011年8月10日星期三,下午11:54,Armin Ronacher 写道:

> On 2011-08-10 5:51 PM, linnchord wrote:
> > thanks, but not this.
> >  
> > DEBUG=false is the first line of my app config.
> Which causes the logging to be disabled. In non-debug mode you have to  
> configure the logger by hand.
>  
>  
> Regards,
> Armin

回复: [flask] no logging when flas

From:
linnchord
Date:
2011-08-11 @ 03:21
hello

At last, I found that.

My app name string 'appname' use in below.

the logger
---------------
logger.get('appname')


deploy path & call .py
--------------------------------
/var/www/appname/appname.py


nginx config
-----------------
uwsgi_param UWSGI_CHDIR /var/www/appname;
uwsgi_param UWSGI_SCRIPT appname:app;
uwsgi_param SERVER_NAME appname;



If the logger define name is not same with call python script name, the 
problem is solved.

So

logger.get('appname_app')

Or

/var/www/appname/main.py

and nginx config: uwsgi_param UWSGI_SCRIPT main:app;

But why? Mybe something override in uwsgi? Is a bug?


-------------------------------
linnchord@gmail.com

已使用 Sparrow (http://www.sparrowmailapp.com/?sig) 发送  

在 2011年8月11日星期四,上午12:23,linnchord 写道:

> hi
>  
> I know it.
>  
> There's a logger.conf in app root
> 
-----------------------------------------------------------------------------------------------
> [loggers]
> keys=root,myapp
>  
>  
> [handlers]
> keys=console,timedRotating
>  
>  
> [formatters]
> keys=baseFormatter
>  
>  
> [logger_root]
> level=DEBUG
> handlers=console
>  
>  
> [logger_myapp]
> handlers=console,timedRotating
> qualname=myapp
> propagate=0
>  
>  
> [handler_console]
> class=StreamHandler
> level=DEBUG
> formatter=baseFormatter
> args=(sys.stdout,)
>  
>  
> [handler_timedRotating]
> class=handlers.TimedRotatingFileHandler
> level=WARNING
> formatter=baseFormatter
> args=('log/myapp.log','d',1,15)
>  
>  
> [formatter_baseFormatter]
> format=%(asctime)s - %(levelname)s: %(message)s
>  
>  
>  
>  
> and a logger.py
> -------------------------------------------------------
> import logging
> import logging.config
>  
> logging.config.fileConfig('logger.conf')
> logger = logging.getLogger('myapp')
>  
>  
>  
>  
> a app config config/production.py
> -----------------------------------------
> DEBUG=False
> …
>  
>  
> load app config in myapp.py
> -----------------------------------------
> from logger import logger
>  
> app = Flask(__name__)
> app.config.from_object('config.production')
>  
>  
>  
> ok, is there something conflict?
>  
>  
> -------------------------------
> linnchord@gmail.com (mailto:linnchord@gmail.com)
>  
> 已使用 Sparrow (http://www.sparrowmailapp.com/?sig) 发送  
>  
> 在 2011年8月10日星期三,下午11:54,Armin Ronacher 写道:
>  
> > On 2011-08-10 5:51 PM, linnchord wrote:
> > > thanks, but not this.
> > >  
> > > DEBUG=false is the first line of my app config.
> > Which causes the logging to be disabled. In non-debug mode you have to  
> > configure the logger by hand.
> >  
> >  
> > Regards,
> > Armin
>  

Re: 回复: [flask] no logging when flas

From:
Roberto De Ioris
Date:
2011-08-11 @ 03:52
> hello
>
> At last, I found that.
>
> My app name string 'appname' use in below.
>
> the logger
> ---------------
> logger.get('appname')
>
>
> deploy path & call .py
> --------------------------------
> /var/www/appname/appname.py
>
>
> nginx config
> -----------------
> uwsgi_param UWSGI_CHDIR /var/www/appname;
> uwsgi_param UWSGI_SCRIPT appname:app;
> uwsgi_param SERVER_NAME appname;
>
>
>
> If the logger define name is not same with call python script name, the
> problem is solved.
>
> So
>
> logger.get('appname_app')
>
> Or
>
> /var/www/appname/main.py
>
> and nginx config: uwsgi_param UWSGI_SCRIPT main:app;
>
> But why? Mybe something override in uwsgi? Is a bug?
>
>

Using UWSGI_SCRIPT (or --module) your app is imported as a module.

When imported as a module the value of __name__ will be the name of the
module instead of '__main__'

This changes again when using --file (instead of --module) and again with
--wsgi-file


-- 
Roberto De Ioris
http://unbit.it

回复: å›žå¤ ï¼š [flask] no logging when flas

From:
linnchord
Date:
2011-08-11 @ 04:15
hi

 What's the point? I don't get it, sorry.

I know the model named by UWSGI_SCRIPT, but the param of 
logging.getLogger('stringstring') is just a string, isn't it?  

What's the relationship between the logger define param and the model?

I check the reference

logging.getLogger([name])

Return a logger with the specified name or, if no name is specified, 
return a logger which is the root logger of the hierarchy. If specified, 
the name is typically a dot-separated hierarchical name like “a”, “a.b” or
“a.b.c.d”. Choice of these names is entirely up to the developer who is 
using logging.




I don't think there's any relationship between them.

-------------------------------
linnchord@gmail.com


在 2011年8月11日星期四,上午11:52,Roberto De Ioris 写道:

>  
> > hello
> >  
> > At last, I found that.
> >  
> > My app name string 'appname' use in below.
> >  
> > the logger
> > ---------------
> > logger.get('appname')
> >  
> >  
> > deploy path & call .py
> > --------------------------------
> > /var/www/appname/appname.py
> >  
> >  
> > nginx config
> > -----------------
> > uwsgi_param UWSGI_CHDIR /var/www/appname;
> > uwsgi_param UWSGI_SCRIPT appname:app;
> > uwsgi_param SERVER_NAME appname;
> >  
> >  
> >  
> > If the logger define name is not same with call python script name, the
> > problem is solved.
> >  
> > So
> >  
> > logger.get('appname_app')
> >  
> > Or
> >  
> > /var/www/appname/main.py
> >  
> > and nginx config: uwsgi_param UWSGI_SCRIPT main:app;
> >  
> > But why? Mybe something override in uwsgi? Is a bug?
>  
> Using UWSGI_SCRIPT (or --module) your app is imported as a module.
>  
> When imported as a module the value of __name__ will be the name of the
> module instead of '__main__'
>  
> This changes again when using --file (instead of --module) and again with
> --wsgi-file
>  
>  
> --  
> Roberto De Ioris
> http://unbit.it

Re: 回复: å›žå ¤ : [flask] no logging when flas

From:
Roberto De Ioris
Date:
2011-08-11 @ 04:24
> hi
>
>  What's the point? I don't get it, sorry.
>
> I know the model named by UWSGI_SCRIPT, but the param of
> logging.getLogger('stringstring') is just a string, isn't it?
>
> What's the relationship between the logger define param and the model?
>
> I check the reference
>
> logging.getLogger([name])
>
> Return a logger with the specified name or, if no name is specified,
> return a logger which is the root logger of the hierarchy. If specified,
> the name is typically a dot-separated hierarchical name like “a”,
> “a.b” or “a.b.c.d”. Choice of these names is entirely up to the
> developer who is using logging.
>

app = Flask(__name__)

will create a logger object (when requested) of name '__main__' when run
by python and another one named 'appname' when run under uWSGI.

Check flask/app.py (the logger() definition) and flask/logger.py and you
will understand the Armin response too.

-- 
Roberto De Ioris
http://unbit.it

回复: å›žå¤ ï¼š 回å ¤ : [flask] no logging when flas

From:
linnchord
Date:
2011-08-11 @ 05:10
hi

At last, I get it, thanks!

The logger will be override by Flask after import and app create because 
of the same name of model name.

So I must init logger after that.

OK, thanks a lot!


-------------------------------
linnchord@gmail.com

已使用 Sparrow (http://www.sparrowmailapp.com/?sig) 发送  

在 2011年8月11日星期四,下午12:24,Roberto De Ioris 写道:

>  
> > hi
> >  
> >  What's the point? I don't get it, sorry.
> >  
> > I know the model named by UWSGI_SCRIPT, but the param of
> > logging.getLogger('stringstring') is just a string, isn't it?
> >  
> > What's the relationship between the logger define param and the model?
> >  
> > I check the reference
> >  
> > logging.getLogger([name])
> >  
> > Return a logger with the specified name or, if no name is specified,
> > return a logger which is the root logger of the hierarchy. If specified,
> > the name is typically a dot-separated hierarchical name like “a”,
> > “a.b” or “a.b.c.d”. Choice of these names is entirely up to the
> > developer who is using logging.
>  
> app = Flask(__name__)
>  
> will create a logger object (when requested) of name '__main__' when run
> by python and another one named 'appname' when run under uWSGI.
>  
> Check flask/app.py (the logger() definition) and flask/logger.py and you
> will understand the Armin response too.
>  
> --  
> Roberto De Ioris
> http://unbit.it