SwiProlog123
SwiProlog123

Reputation: 11

Django logging with dynamic name

I'm trying to create logs in my django project, but i want a log with a different filename for each execution. What i did so far is set it up like it's written here, but this creates one log and puts everything in it. I want a log per execution with a dynamic name, like instance name + date or something. Can this be done?

Edit: Code example of creating a scheduled task. (I think you will get what it does from this)

def create_schedule (inst):


     path_task=os.path.join(os.path.join(os.path.dirname(__file__),os.pardir),os.pardir)
     path_task=path_task[:len(path_task)-2]+'executeInstance.py'
     tr='"py '+path_task + ' ' + str(inst.id)+'"'
     tn='Inst ' + str(inst.id)

     time=inst.schedule.time


     if inst.schedule.scheduleType=='Weekly':

        w_days=''
        for day in inst.schedule.weekDays.all():
                if w_days!='':
                   w_days=w_days+','
                w_days=w_days+day.weekDay[:3].upper()

        tn='"'+tn+'"'
        cmd_sch='schtasks /create /tn ' + tn + ' /tr ' + tr + ' /sc weekly /d '+ w_days + ' /st ' + time + ' /RU SYSTEM'
        result = subprocess.check_output(cmd_sch, shell=True)

And then catching an exception in executeInstance.py

    except smtplib.SMTPDataError as e:

            status_log='Error'
            error_desc='Message size too big for your email server'


            inst=Instance.objects.get(id=instance_id)
            log=Log(instance=inst,status=status_log,errorDesc=error_desc,executionDate=datetime.now())
            log.save()
            logger.error(log)

Upvotes: 1

Views: 973

Answers (1)

eran
eran

Reputation: 6921

Maybe try to play with time rotated log files? https://docs.python.org/2.7/library/logging.handlers.html#rotatingfilehandler

Upvotes: 1

Related Questions