⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logwriter.pas

📁 本人实现得一个系统运行日志记录类
💻 PAS
字号:
unit LogWriter;

interface
uses
    Messages, classes, SysUtils, Windows, Dialogs;

const
    FILENAME = 'Logger';
type
{
This is a copy-paste from the java documentation.
It describes the use of logging levels

---------------------------------------------------------------
SEVERE
is a message level indicating a serious failure.
In general SEVERE messages should describe events that are of
considerable importance and which will prevent normal program
execution. They should be reasonably intelligible to end users
and to system administrators.
---------------------------------------------------------------
WARNING
is a message level indicating a potential problem. 
In general WARNING messages should describe events that will
be of interest to end users or system managers, or which
indicate potential problems.
---------------------------------------------------------------
INFO
is a message level for informational messages. 
Typically INFO messages will be written to the console or its
equivalent. So the INFO level should only be used for
reasonably significant messages that will make sense to end
users and system admins.
---------------------------------------------------------------
CONFIG
is a message level for static configuration messages.
CONFIG messages are intended to provide a variety of static
configuration information, to assist in debugging problems
that may be associated with particular configurations.
For example, CONFIG message might include the CPU type, the
graphics depth, the GUI look-and-feel, etc.
---------------------------------------------------------------
FINE
is a message level providing tracing information. 
All of FINE, FINER, and FINEST are intended for relatively
detailed tracing. The exact meaning of the three levels will
vary between subsystems, but in general, FINEST should be used
for the most voluminous detailed output, FINER for somewhat
less detailed output, and FINE for the lowest volume (and most
important) messages. 
In general the FINE level should be used for information that
will be broadly interesting to developers who do not have a
specialized interest in the specific subsystem.
FINE messages might include things like minor (recoverable)
failures. Issues indicating potential performance problems are
also worth logging as FINE.
---------------------------------------------------------------
FINER
indicates a fairly detailed tracing message. By default
logging calls for entering, returning, or throwing an
exception are traced at this level.
---------------------------------------------------------------
FINEST
public static final Level FINESTFINEST indicates a highly
detailed tracing message.
---------------------------------------------------------------
}
    TLogLevel = (
        LEVEL_OFF,
		    LEVEL_SEVERE,
		    LEVEL_WARNING,
		    LEVEL_INFO,
		    LEVEL_CONFIG,
		    LEVEL_FINE,
		    LEVEL_FINER,
		    LEVEL_FINEST
    );      //日志级别

    TLogWriter = Class
    private
		    m_csLogFileSync : TRTLCriticalSection;
		    m_strLogFileName : string;
        function GetLevelNames(level : TLogLevel) : string;
    public
        constructor Create;
        destructor Destroy; override;
		    procedure Log(level :TLogLevel ; msg : string);
		    procedure RemoveLastMonthFile();
		    procedure SetLogFileName(strLogFileName : string);
    end;
    function GetLogger() : TLogWriter;
    procedure FINEST_LOG (msg : string);
    procedure FINER_LOG  (msg : string);
    procedure CONFIG_LOG (msg : string);
    procedure INFO_LOG   (msg : string);
    procedure WARNING_LOG(msg : string);
    procedure SEVERE_LOG (msg : string);

implementation
var
    m_Logger : TLogWriter ;

{ TLogWriter }

constructor TLogWriter.Create;
begin
    InitializeCriticalSection(m_csLogFileSync);
    m_strLogFileName := FILENAME;
end;

destructor TLogWriter.Destroy;
begin
    DeleteCriticalSection(m_csLogFileSync);
    inherited Destroy;
end;

function TLogWriter.GetLevelNames(level: TLogLevel): string;
begin
    Result := '';
    case level of
    LEVEL_OFF     : Result := '[OFF]    ';
    LEVEL_SEVERE  : Result := '[SEVERE] ';
    LEVEL_WARNING : Result := '[WARNING]';
    LEVEL_INFO    : Result := '[INFO]   ';
    LEVEL_CONFIG  : Result := '[CONFIG] ';
    LEVEL_FINE    : Result := '[FINE]   ';
    LEVEL_FINER   : Result := '[FINER]  ';
    LEVEL_FINEST  : Result := '[FINEST] ';
    end;
end;

procedure TLogWriter.Log(level: TLogLevel; msg: string);
var
    strMessage : string;
    strFileName : string;
    hFile : THandle;
    systime : _SYSTEMTIME;
    dwNumBytesWritten : DWORD;
    LogFile : TextFile;
begin
    if (level > LEVEL_FINEST) then exit;

	  EnterCriticalSection(m_csLogFileSync);


    strFileName := m_strLogFileName + FormatDateTime('yyyymmdd',Now) + '.txt';

    dwNumBytesWritten := 0;
    AssignFile(LogFile, strFileName);
    if fileExists(strFileName) then
        Append(LogFile)
    else
        Rewrite(LogFile);
    try
        GetLocalTime(systime);

        strMessage := Format('%04d-%02d-%02d %02d:%02d:%02d.%03d ', [systime.wYear,
                             systime.wMonth, systime.wDay , systime.wHour,
                             systime.wMinute, systime.wSecond, systime.wMilliseconds])
                    + GetLevelNames(level) + ' : ' + msg;

        Writeln(LogFile, strMessage);
    finally
        CloseFile(LogFile);
    end;

	  RemoveLastMonthFile();

	  LeaveCriticalSection(m_csLogFileSync);
end;

procedure TLogWriter.RemoveLastMonthFile;
begin

end;

procedure TLogWriter.SetLogFileName(strLogFileName: string);
begin
	  EnterCriticalSection(m_csLogFileSync);

    m_strLogFileName := strLogFileName;

	  LeaveCriticalSection(m_csLogFileSync);
end;

function GetLogger() : TLogWriter;
begin
    result := TLogWriter.Create;
end;

procedure FINEST_LOG(msg : string);
begin
    GetLogger.Log(LEVEL_FINEST, msg);
end;

procedure FINER_LOG(msg : string);
begin
   GetLogger.Log(LEVEL_FINER, msg);
end;

procedure CONFIG_LOG(msg : string);
begin
   GetLogger.Log(LEVEL_CONFIG, msg);
end;

procedure INFO_LOG(msg : string);
begin
   GetLogger.Log(LEVEL_INFO, msg);
end;

procedure WARNING_LOG(msg : string);
begin
   GetLogger.Log(LEVEL_WARNING, msg);
end;
procedure SEVERE_LOG(msg : string);
begin
   GetLogger.Log(LEVEL_SEVERE, msg);
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -