Deserialize in a different language

前端 未结 6 1752
無奈伤痛
無奈伤痛 2021-01-02 21:46

The log4j network adapter sends events as a serialised java object. I would like to be able to capture this object and deserialise it in a different language (python). Is th

6条回答
  •  感情败类
    2021-01-02 22:11

    In theory it's possible. Now how difficult in practice it might be depends on whether Java serialization format is documented or not. I guess, it's not. edit: oops, I was wrong, thanks Charles.

    Anyway, this is what I suggest you to do

    1. capture from log4j & deserialize Java object in your own little Java program.

    2. now when you have the object again, serialize it using your own custom formatter.

      Tip: Maybe you don't even have to write your own custom formatter. for example, JSON (scroll down for libs) has libraries for Python and Java, so you could in theory use Java library to serialize your objects and Python equivalent library to deserialize it

    3. send output stream to your python application and deserialize it

    Charles wrote:

    the problem is that for this to work, your "little java program" needs to load the same versions of all the same classes that it might deserialize. Which is tricky if you're receiving log messages from one app, and really tricky if you're multiplexing more than one log stream. Either way, it's not going to be a little program any more.

    Can't you just simply reference Java log4j libraries in your own java process? I'm just giving general advice here that is applicable to any pair of languages (name of the question is pretty language agnostic so I just provided one of the generic solutions). Anyway, I'm not familiar with log4j and don't know whether you can "inject" your own serializer into it. If you can, then of course your suggestion is much better and cleaner.

提交回复
热议问题