Answers for "create log file"

6

making log files in python

import logging
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logging.debug('This is debug message')
logging.info('This is information message')
logging.warning('This is warning message')
logging.error('This is warning message')
Posted by: Guest on July-12-2021
0

create new logfile with some initial text in logback

public class FileAppenderWithHeader extends FileAppender {

    private String header;

    public String getHeader() {
        return header;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    @Override
    public void openFile(String fileName) throws IOException {
        super.openFile(fileName);
        File activeFile = new File(getFile());
        if (activeFile.exists() && activeFile.isFile() && activeFile.length() == 0) {
            write((header + "n").getBytes());
        }
    }

    private void write(byte[] byteArray) throws IOException {
        if (byteArray == null || byteArray.length == 0)
            return;

        lock.lock();
        try {
            super.getOutputStream().write(byteArray);
            if (super.isImmediateFlush()) {
                super.getOutputStream().flush();
            }
        } finally {
            lock.unlock();
        }
    }
}
Posted by: Guest on February-05-2021

Python Answers by Framework

Browse Popular Code Answers by Language