如何使用Log4J在Java中创建二进制日志文件[英] How to Create Binary Log File in Java using Log4J

本文是小编为大家收集整理的关于如何使用Log4J在Java中创建二进制日志文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我能够使用fileappender,rollingfileappender等创建日志文件

我的问题是,日志被写成纯文本,任何人都可以阅读,但我想将日志注册为不可读取的二进制文件.

任何人都可以帮助我提出为示例代码创建二进制日志文件的建议.

推荐答案

做您写的事情不是一个好主意,但是如果您真的需要,请写下这样的应用程序:

package de.steamnet.loggingUtils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

public class BinaryAppender extends AppenderSkeleton {

    FileOutputStream fout;

    public BinaryAppender() throws FileNotFoundException {
        fout = new FileOutputStream("/tmp/somefile.log.bin");
    }

    @Override
    protected void append(LoggingEvent le) {
        String origMessage = le.getLoggerName() + " said: " + le.getMessage();
        byte[] obscure = origMessage.getBytes();
        for(int ii = 0; ii < obscure.length; ii++) {
            if(obscure[ii] == Byte.MAX_VALUE) {
                obscure[ii] = Byte.MIN_VALUE;
            } else {
                obscure[ii] = (byte)(obscure[ii] +1); // thats a really bad idea to create 'nonesense stuff' that way!
            }
        }
        try {
            fout.write(obscure);
        } catch (IOException ex) {
            System.out.println("too bad. File writer bombed.");
        }
    }

    @Override
    public boolean requiresLayout() {
        return false; // we do all layouting in here.
    }

    @Override
    public void close() {
        try {
            fout.close();
        } catch (IOException ex) {
            System.out.println("too bad. could not close it.");
        }
    }

}

然后在您的log4j配置中使用此类作为appender,您就完成了写作部分.对于读取部分,您需要再次读取每个字节并将字节减少一个字节,然后从中加载一个字符串.

祝你好运.

其他推荐答案

我将使用dataOutputStream bufferedoutputstream和fileOutputstream来编写二进制文件.我认为您希望这些文件是可读取的,而不是不可读取的. ;)

log4j专为文本文件而设计,但是您可以使用其记录级别.

private static final Log LOG = 
static DataOutputStream out = ... FileOutputStream ...

if(LOG.isDebugEnabled()) {
   // write a debug log to out
}

本文地址:https://www.itbaoku.cn/post/1575037.html

问题描述

I am able to create log files using FileAppender, RollingFileAppender,etc.,

My Problem is that the logs are written as plain text that anyone can read, but I want to register my logs as Binary Files that are not human readable.

Can anyone help me with the suggestion to create a Binary log file for an example code.

推荐答案

Its not such a good Idea to do what you wrote, but if you really need to, write an own appender like this:

package de.steamnet.loggingUtils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;

public class BinaryAppender extends AppenderSkeleton {

    FileOutputStream fout;

    public BinaryAppender() throws FileNotFoundException {
        fout = new FileOutputStream("/tmp/somefile.log.bin");
    }

    @Override
    protected void append(LoggingEvent le) {
        String origMessage = le.getLoggerName() + " said: " + le.getMessage();
        byte[] obscure = origMessage.getBytes();
        for(int ii = 0; ii < obscure.length; ii++) {
            if(obscure[ii] == Byte.MAX_VALUE) {
                obscure[ii] = Byte.MIN_VALUE;
            } else {
                obscure[ii] = (byte)(obscure[ii] +1); // thats a really bad idea to create 'nonesense stuff' that way!
            }
        }
        try {
            fout.write(obscure);
        } catch (IOException ex) {
            System.out.println("too bad. File writer bombed.");
        }
    }

    @Override
    public boolean requiresLayout() {
        return false; // we do all layouting in here.
    }

    @Override
    public void close() {
        try {
            fout.close();
        } catch (IOException ex) {
            System.out.println("too bad. could not close it.");
        }
    }

}

Then in your log4j config use this class as appender and you are done with the writing part. for the reading part you again need to read it byte per byte and reduce the byte by one and then load a string from it.

Good luck.

其他推荐答案

I would use DataOutputStream BufferedOutputStream and FileOutputStream to write binary files. I assume you want the files to be machine readable rather than non-readable. ;)

Log4j is designed for text files, however you can use its logging levels like.

private static final Log LOG = 
static DataOutputStream out = ... FileOutputStream ...

if(LOG.isDebugEnabled()) {
   // write a debug log to out
}