<div dir="ltr">Thanks!  So I tried something like this:<div><br></div><div>from flask import current_app</div><div>class MyObject:</div><div>  def __init__(self):</div><div>    self.logger = current_app.logger</div><div>    <a href="http://self.logger.info">self.logger.info</a>('sdfdf")</div><div><br></div><div>And now I'm getting something like this:</div><div>ERROR/MainProcess] Task main.async_process_on_startup[27608e0c-c9a5-4ef0-ac47-244759acd465] raised unexpected: RuntimeError('Working outside of application context.\n\nThis typically means that you attempted to use functionality that needed\nto interface with the current application object in a way.  To solve\nthis set up an application context with app.app_context().  See the\ndocumentation for more information.',)<br></div><div><br></div><div>Any idea what I did wrong?</div><div><br></div><div>Thanks again!!</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 8, 2016 at 5:49 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">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 examples.<br>
<br>
1. Use the current_app object. In the module defining your object you can 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>
<div class="HOEnZb"><div class="h5"><br>
<br>
* On Thursday, July 07, 2016 at 05:27 PM -0400, Guy Matz <<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?  Do<br>
> I need to pass in the logger to the new object?  Or is there a better way?<br>
><br>
> Thanks!<br>
> Guy<br>
</div></div></blockquote></div><br></div>