<div dir="ltr"><font color="#000000"><span style="font-size:12.8px">Expanding on Ziirish's reply, I do the following:</span></font><div><font color="#000000"><span style="font-size:12.8px"><br></span></font></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><span style="font-family:monospace,monospace">import logging</span><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><font face="monospace, monospace"><br></font><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><span style="font-family:monospace,monospace">class MyObject(object):</span><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><span style="font-family:monospace,monospace">    def __init__(self, logger=None):</span><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><span style="font-family:monospace,monospace">        self.logger = logger or logging.getLogger(__name__)</span><div><font face="monospace, monospace"><br></font><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><span style="font-family:monospace,monospace">    def hello(self):</span><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><span style="font-family:monospace,monospace">        <a href="http://self.logger.info">self.logger.info</a>('hi')</span><br><blockquote style="margin:0 0 0 40px;border:none;padding:0px"></blockquote><font face="monospace, monospace"><br></font><font face="monospace, monospace">my = MyObject(logger=current_app.logger)</font><div><font color="#000000"><br></font></div></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><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>_______________________________________________<br>
Flask mailing list<br>
<a href="mailto:Flask@python.org">Flask@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/flask" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/flask</a><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Scott Werner<br><a href="mailto:scott.werner.vt@gmail.com" target="_blank">scott.werner.vt@gmail.com</a><br></div></div></div>
</div>