1



I'm trying to log changes in our CMS to a logging file. I make use of the def logging_on_activity(). What I want is a new file for each day to keep the log records a bit organised.

I've already tried to use the datetime package to manually edit the filename.

def logging_on_activity(platform, appname, msg):
    import datetime
    now = datetime.datetime.now()

    if SITE_TYPE == SiteType.LOCAL:
        filename = 'path_to/../logs/' + str(now.__format__('%Y')) + '/' 
            + str(now.__format__('%m')) + '/' \
            + str(now.__format__('%d')) + '/logging.log'

    import datetime
    date = datetime.date.today().isoformat()
    time = datetime.datetime.now().strftime('%H:%m:%S')

    # Write to file
    with open(filename, 'a') as handle:
        handle.write("{0} {1} [ii] [{2:^19}] [{3:^19}]  {4}\n".format(date, time, platform, appname, msg))

The error I get is:

FileNotFoundError at ..
[Errno 2] No such file or directory: 'itdb_tools/logs/2019/09/11.log'

Example of what I want?
Date: 2019/09/11 -> System is writing to: path_to/../logs/2019/09/11/logging.log
Date: 2019/09/12 -> System is writing to: path_to/../logs/2019/09/12/logging.log

I hope what I want to achieve is possible.

Thanks in advance and I hope someone can help me in the right direction!

W. White
  • 127
  • 13

2 Answers2

2

You have to create each new directory too. So you have to create the directory 2019, the directory 2019/09, the directory 2019/09/12, the directory 2019/09/11, etc. Python open can't just create directories on the fly implicitly. It only creates the leaf notes in the path.

Ian Kirkpatrick
  • 1,861
  • 14
  • 33
  • 2
    You would be able to use `os.makedirs` to create all the directories in one go. – cenh Sep 11 '19 at 13:17
  • @cenh Do I have to run a certain script to create all the directories at once or do I have to include the os.makedirs method in my code and it will create them on the fly? – W. White Sep 11 '19 at 13:27
  • 1
    @W.White you would have to include `os.makedirs`, it takes the folder structure as input so i.e. you could `os.makedirs('path_to/../logs/2019/09/11/')` and it would make all the folders for you. – cenh Sep 11 '19 at 13:30
0

I think it would be easier if you create the desired directory with os.makedirs(path). After that you can create your files each directory.

import datetime
now = datetime.datetime.now()

pathname = 'your desired directory' + str(now.__format__('%Y')) + '\\' + str(now.__format__('%m')) + '\\' + str(now.__format__('%d')) + "\\"
filename = pathname+ 'logging.log'

import datetime
date = datetime.date.today().isoformat()
time = datetime.datetime.now().strftime('%H:%m:%S')

# Write to file
try:
    os.makedirs(pathname)
    with open(filename, 'w') as handle:
        handle.write("{0} {1} [ii] [{2:^19}] [{3:^19}]  {4}\n".format(date, time, platform, appname, msg))
except Exception as e:
    with open(filename, 'w') as handle:
        handle.write("{0} {1} [ii] [{2:^19}] [{3:^19}]  {4}\n".format(date, time, platform, appname, msg))
0xM4x
  • 460
  • 1
  • 8
  • 19