<div dir="ltr">Silly me.  I am creating a flask app that uses celery, so I actually needed the logger from celery in this case.  Sorry for the bother!</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 8, 2016 at 11:39 AM, Ziirish <span dir="ltr"><<a href="mailto:ziirish@ziirish.info" target="_blank">ziirish@ziirish.info</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Well, the error speaks for itself.<br>
<br>
How is your object instantiated/called in your application?<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
<br>
* On Friday, July 08, 2016 at 11:22 AM -0400, Guy Matz <<a href="mailto:guymatz@gmail.com">guymatz@gmail.com</a>> wrote:<br>
> Thanks!  So I tried something like this:<br>
><br>
> from flask import current_app<br>
> class MyObject:<br>
>   def __init__(self):<br>
>     self.logger = current_app.logger<br>
>     <a href="http://self.logger.info" rel="noreferrer" target="_blank">self.logger.info</a>('sdfdf")<br>
><br>
> And now I'm getting something like this:<br>
> ERROR/MainProcess] Task<br>
> main.async_process_on_startup[27608e0c-c9a5-4ef0-ac47-244759acd465] raised<br>
> unexpected: RuntimeError('Working outside of application context.\n\nThis<br>
> typically means that you attempted to use functionality that needed\nto<br>
> interface with the current application object in a way.  To solve\nthis set<br>
> up an application context with app.app_context().  See the\ndocumentation<br>
> for more information.',)<br>
><br>
> Any idea what I did wrong?<br>
><br>
> Thanks again!!<br>
><br>
> On Fri, Jul 8, 2016 at 5:49 AM, Ziirish <<a href="mailto:ziirish@ziirish.info">ziirish@ziirish.info</a>> wrote:<br>
><br>
> > Hello,<br>
> ><br>
> > As usual, there is more than one way to do it.<br>
> > I don't know what is the best way to do it though so here are a few<br>
> > examples.<br>
> ><br>
> > 1. Use the current_app object. In the module defining your object you can<br>
> > do<br>
> > this:<br>
> ><br>
> > from flask import current_app<br>
> ><br>
> > class MyObject:<br>
> >     def foo(self):<br>
> >         <a href="http://current_app.logger.info" rel="noreferrer" target="_blank">current_app.logger.info</a>('bar')<br>
> ><br>
> ><br>
> > 2. Like you suggested, you could pass to your object the flask app (that's<br>
> > basically what most of the flask extensions do):<br>
> ><br>
> > class MyObject:<br>
> >     def __init__(self, app=None):<br>
> >         self.app = app<br>
> ><br>
> >     def init_app(self, app):<br>
> >         self.app = app<br>
> ><br>
> >     def foo(self):<br>
> >         <a href="http://self.app.logger.info" rel="noreferrer" target="_blank">self.app.logger.info</a>('bar')<br>
> ><br>
> ><br>
> > There are probably other ways but those are the two most common IMHO.<br>
> ><br>
> ><br>
> > * On Thursday, July 07, 2016 at 05:27 PM -0400, Guy Matz <<br>
> > <a href="mailto:guymatz@gmail.com">guymatz@gmail.com</a>> wrote:<br>
> > > Hi!  Can anyone recommend a way to log from objects that are not flask<br>
> > > objects?  I.e. if my flask code creates a new object that does some<br>
> > > processing, etc., how can I get access to the logger from that object?<br>
> > Do<br>
> > > I need to pass in the logger to the new object?  Or is there a better<br>
> > way?<br>
> > ><br>
> > > Thanks!<br>
> > > Guy<br>
> ><br>
</div></div></blockquote></div><br></div>