Can you shorten logging headers when using python logging `%(funcName)s`?
<p>Sometimes I configure the python logging formatter using the <code>%(funcName)s</code>. But I don't like this when the function names are really long.</p>

<p><strong>Can you shorten logging headers when using python logging <code>%(funcName)s</code>? If yes, how?</strong></p>

<p>Can you say... limit the total number of characters to like 10 characters?</p>

<p>You can extend the <code>logging.formatter</code> class to get the desired formatting like below.</p>

<p>This is just an example. you should modify it based on your requirement
class MyFormatter(logging.Formatter):
    in_console = False
    def <strong>init</strong>(self):
        self.mod_width = 30
        self.datefmt = '%H:%M:%S'</p>

<pre><code>def format(self, record):
    s = []
    cmodule = record.module + ":" + str(record.lineno)
    cmodule = cmodule[-self.mod_width:].ljust(self.mod_width)
    format_str = ("%-7s %s %s" % (record.levelname, self.formatTime(record, self.datefmt), cmodule))
    pad_len = len(format_str)
    for line in record_msg[1:]:
        line =  " " * pad_len + " : " + line
        s.append(line)

    s = "%s : %s" % (format_str, "\n".join(s))
    return s

my_formatter = MyFormatter()
log.setFormatter(my_formatter)
</code></pre>

python logging instrumentation