Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9165079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:49:53+00:00 2026-06-17T14:49:53+00:00

I’m using python logging and I have a formatter that looks like the following:

  • 0

I’m using python logging and I have a formatter that looks like the following:

formatter = logging.Formatter(
    '%(asctime)s - %(pathname)86s - %(lineno)4s - %(message)s', '%d %H:%M'
    )

As you can see, I like the information in my log files to line up neatly in columns. The reason I have 86 spaces reserved for the pathname is because the full paths to some of the files used in my program are that long. However, all I really need is the actual file name, not the full path. How can I get the logging module to give me just the file name? Better yet, since I have some long filenames, I’d like the first 3 characters of the filename, followed by ‘~’, then the last 16 characters. So

/Users/Jon/important_dir/dev/my_project/latest/testing-tools/test_read_only_scenarios_happily.py

should become

tes~arios_happily.py
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:49:55+00:00Added an answer on June 17, 2026 at 2:49 pm

    You’ll have to implement your own Formatter subclass that truncates the path for you; the formatting string cannot do this.

    Since you want to use just the filename you’d want to use the filename parameter as the base, which we can then truncate as needed.

    For the size-limited version, I’d actually use a new record attribute for, leaving the existing filename to be the full name. Lets call it filename20.

    import logging
    
    class FilenameTruncatingFormatter(logging.Formatter):
        def format(self, record):
            # truncate the filename if it is longer than 20 characters
            filename = record.filename
            if len(filename) > 20:
                filename = '{}~{}'.format(filename[:3], filename[-16:])
            record.filename20 = filename
            return super(PathTruncatingFormatter, self).format(record)
    

    Use this class instead of the normal logging.Formatter instance, and reference the filename20 field in the formatting string:

    formatter = FilenameTruncatingFormatter(
        '%(asctime)s - %(filename20)20s - %(lineno)4s - %(message)s', '%d %H:%M'
    )
    

    Note that I also shortened the space needed for the field to 20 characters: %(...)20s.

    If you have to specify the formatter in a ConfigParser-style configuration file, create a named formatter via a [formatter_NAME] section, and give that section a class key:

    [formatter_truncated]
    class=some.module.FilenameTruncatingFormatter
    format=%(asctime)s - %(filename20)20s - %(lineno)4s - %(message)s
    datefmt=%d %H:%M
    

    and then you can use formatter=truncated in any handler sections you define.

    For the dictionary-schema style configuration, the same applies: specify the formatter class via the class key in any formatter definition.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.