logging-Modul: DailyRollingFileHandler

Code-Stücke können hier veröffentlicht werden.
Antworten
devnull
User
Beiträge: 3
Registriert: Montag 10. Januar 2005, 21:18

Montag 10. Januar 2005, 21:21

Hallo!

Mir fehlte im Logging-Modul der DailyRollingFileHandler wie in log4j etc. besitzen. Daher habe ich ihn mir selbst geschrieben auf Basis des RotatingFileHandlers.

Falls es sowas schon gegeben haben sollte, wuerde ich mich ueber den Link freuen. Falls nicht, hoffe ich, dass mein Code nuetzlich ist :)

Code: Alles auswählen

import logging
from datetime import date
import string


class DailyRollingFileHandler(logging.FileHandler):
    def __init__(self, filename, mode="a"):
        """
        Open the specified file and use it as the stream for logging.

        Rollover occurs whenever the day changes.

        The names of the log files each contain the date when they were
        created. Thus if the filename "myapp.log" was used, the log files
        each have look like myapp-2005-01-07.log etc.

        The date is inserted at the position of the last '.' in the filename
        if any or simply appended to the given name if no dot was present.
        """
        self.currentDay   = date.today()
        # create the logfile name parts (base part and extension)
        nameparts     = string.split(string.strip(filename), ".")
        self.filestub = ""
        self.fileext  = ""

        # remove empty items
        while nameparts.count("") > 0:
            nameparts.remove("")

        if len(nameparts) < 2:
            self.filestub = nameparts[0]
        else:
            # construct the filename
            for part in nameparts[0:-2]:
                self.filestub += part + "."
            self.filestub += nameparts[-2]
            self.fileext   = "." + nameparts[-1]
            
        logging.FileHandler.__init__(self, self.getFilename(), mode)

    def getFilename(self):
        return self.filestub + "-" + self.currentDay.isoformat() + self.fileext

    def doRollover(self):
        """
        Do a rollover, as described in __init__().
        """

        self.stream.close()
        self.currentDay   = date.today()        
        self.baseFilename = self.getFilename()
        self.stream = open(self.baseFilename, "w")

    def emit(self, record):
        """
        Emit a record.

        Output the record to the file, catering for rollover as described
        in doRollover().
        """
        msg = "%s\n" % self.format(record)
        self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature
        if date.today() != self.currentDay:
            self.doRollover()
        logging.FileHandler.emit(self, record)
fs111
User
Beiträge: 170
Registriert: Samstag 15. November 2003, 11:42
Kontaktdaten:

Dienstag 11. Januar 2005, 10:11

Unter welcher Lizenz steht der Code?

fs111
Pydoc-Integration in vim - Feedback willkommen: http://www.vim.org/scripts/script.php?script_id=910
devull

Dienstag 11. Januar 2005, 11:43

fs111 hat geschrieben:Unter welcher Lizenz steht der Code?
Darueber habe ich mir noch keine Gedanken gemacht ehrlichgesagt. Wieso?
Leonidas
Administrator
Beiträge: 16024
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Dienstag 11. Januar 2005, 15:11

Weil wenn es unter der richtigen Lizenz ist, könne man es in Python einfließen lassen.

Wenn es unter GPL ist nicht, also am besten Python Lizenz.
My god, it's full of CARs! | Leonidasvoice vs Modvoice
devnull
User
Beiträge: 3
Registriert: Montag 10. Januar 2005, 21:18

Dienstag 11. Januar 2005, 16:04

Leonidas hat geschrieben:Weil wenn es unter der richtigen Lizenz ist, könne man es in Python einfließen lassen..
ok, das wuerde mich natuerlich freuen.
Dann habe ich noch zwei Fragen:

1. Wie kennzeichne ich, dass der Code unter einer bestimmten Lizenz steht?
2. Wie laesst man sowas in das Projekt einfliessen?
fs111
User
Beiträge: 170
Registriert: Samstag 15. November 2003, 11:42
Kontaktdaten:

Dienstag 11. Januar 2005, 16:11

Schreib einfach die Lizenz in den Kopf der Datei.

fs111
Pydoc-Integration in vim - Feedback willkommen: http://www.vim.org/scripts/script.php?script_id=910
devnull
User
Beiträge: 3
Registriert: Montag 10. Januar 2005, 21:18

Dienstag 11. Januar 2005, 17:32

fs111 hat geschrieben:Schreib einfach die Lizenz in den Kopf der Datei.

fs111
Here we go:

Code: Alles auswählen

# Copyright 2004-2005 by Stephan Stapel <stephan.stapel@web.de>. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of Stephan Stapel
# not be used in advertising or publicity pertaining to distribution
# of the software without specific, written prior permission.
#
# STEPHAN STAPEL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
# STEPHAN STAPEL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import logging
from datetime import date
import string
    
class DailyRollingFileHandler(logging.FileHandler):
    """
    The class is based on the standard RotatingFileHandler class from the
    official logging module.

    It rolls over each day, thus one log file per day is created in the form
    myapp-2005-01-07.log, myapp-2005-01-08.log etc.
    """
    
    def __init__(self, filename, mode="a"):
        """
        Open the specified file and use it as the stream for logging.

        Rollover occurs whenever the day changes.

        The names of the log files each contain the date when they were
        created. Thus if the filename "myapp.log" was used, the log files
        each have look like myapp-2005-01-07.log etc.

        The date is inserted at the position of the last '.' in the filename
        if any or simply appended to the given name if no dot was present.
        """
        self.currentDay   = date.today()
        # create the logfile name parts (base part and extension)
        nameparts     = string.split(string.strip(filename), ".")
        self.filestub = ""
        self.fileext  = ""

        # remove empty items
        while nameparts.count("") > 0:
            nameparts.remove("")

        if len(nameparts) < 2:
            self.filestub = nameparts[0]
        else:
            # construct the filename
            for part in nameparts[0:-2]:
                self.filestub += part + "."
            self.filestub += nameparts[-2]
            self.fileext   = "." + nameparts[-1]
            
        logging.FileHandler.__init__(self, self.getFilename(), mode)

    def getFilename(self):
        return self.filestub + "-" + self.currentDay.isoformat() + self.fileext

    def doRollover(self):
        """
        Do a rollover, as described in __init__().
        """

        self.stream.close()
        self.currentDay   = date.today()        
        self.baseFilename = self.getFilename()
        self.stream = open(self.baseFilename, "w")

    def emit(self, record):
        """
        Emit a record.

        Output the record to the file, catering for rollover as described
        in doRollover().
        """
        msg = "%s\n" % self.format(record)
        self.stream.seek(0, 2)  #due to non-posix-compliant Windows feature
        if date.today() != self.currentDay:
            self.doRollover()
        logging.FileHandler.emit(self, record)
So muesste es passen, oder?
Falls ja: Wie submitted man es fuer das offizielle Logging-Modul?
Leonidas
Administrator
Beiträge: 16024
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Dienstag 11. Januar 2005, 17:44

Ja, wenn du lieber die MIT Lizenz magst, okay.
Submitten: in die Python-dev ein announcement posten und/oder einen patch im Python Tracker submitten und ggf die Diskussion mitverfolgen.
My god, it's full of CARs! | Leonidasvoice vs Modvoice
Antworten