<p>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 this possible?</p>

<p><em>NOTE</em> The network capturing is easy; its just a TCP socket and reading in a stream. The difficulty is the deserialising part</p>

[{'answer_id': 29524, 'author': 'Charles Miller', 'author_id': 3052, 'author_profile': 'https://Stackoverflow.com/users/3052', 'pm_score': 4, 'selected': True, 'text': '<p>Generally, no.</p>\n\n<p>The stream format for Java serialization is defined <a href="http://java.sun.com/j2se/1.4/pdf/serial-spec.pdf" rel="nofollow noreferrer">in this document</a>, but you need access to the original class definitions (and a Java runtime to load them into) to turn the stream data back into something approaching the original objects. For example, classes may define writeObject() and readObject() methods to customise their own serialized form.</p>\n\n<p>(<strong>edit:</strong> lubos hasko suggests having a little java program to deserialize the objects in front of Python, but 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. <strong>edit2:</strong> I could be wrong here, I don\'t know what gets serialized. If it\'s just log4j classes you should be fine. On the other hand, it\'s possible to log arbitrary exceptions, and if they get put in the stream as well my point stands.)</p>\n\n<p>It would be much easier to customise the log4j network adapter and replace the raw serialization with some more easily-deserialized form (for example you could use XStream to turn the object into an XML representation)</p>\n'}, {'answer_id': 29529, 'author': 'lubos hasko', 'author_id': 275, 'author_profile': 'https://Stackoverflow.com/users/275', 'pm_score': 1, 'selected': False, 'text': '<p>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. <strong>edit:</strong> <a href="http://java.sun.com/j2se/1.4/pdf/serial-spec.pdf" rel="nofollow noreferrer">oops, I was wrong, thanks Charles</a>.</p>\n\n<p><strong>Anyway, this is what I suggest you to do</strong></p>\n\n<ol>\n<li><p>capture from log4j &amp; deserialize Java object in your own little Java program.</p></li>\n<li><p>now when you have the object again, serialize it using your own custom formatter. </p>\n\n<p><strong><em>Tip:</strong> Maybe you don\'t even have to write your own custom formatter. for example, <a href="http://www.json.org/" rel="nofollow noreferrer">JSON (scroll down for libs)</a> 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</em></p></li>\n<li><p>send output stream to your python application and deserialize it</p></li>\n</ol>\n\n<blockquote>\n  <p><strong>Charles wrote:</strong></p>\n  \n  <p>the problem is that for this\n  to work, your "little java program"\n  needs to load the same versions of all\n  the same classes that it might\n  deserialize. Which is tricky if you\'re\n  receiving log messages from one app,\n  and really tricky if you\'re\n  multiplexing more than one log stream.\n  Either way, it\'s not going to be a\n  little program any more.</p>\n</blockquote>\n\n<p>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.</p>\n'}, {'answer_id': 29535, 'author': 'SCdF', 'author_id': 1666, 'author_profile': 'https://Stackoverflow.com/users/1666', 'pm_score': 1, 'selected': False, 'text': '<p>I would recommend moving to a third-party format (by creating your own log4j adapters etc) that both languages understand and can easily marshal / unmarshal, e.g. XML.</p>\n'}, {'answer_id': 29564, 'author': 'Jörg W Mittag', 'author_id': 2988, 'author_profile': 'https://Stackoverflow.com/users/2988', 'pm_score': 2, 'selected': False, 'text': '<p><em>Theoretically</em>, it\'s possible. The Java Serialization, like pretty much everything in Javaland, is standardized. So, you <em>could</em> implement a deserializer according to that standard in Python. However, the Java Serialization format is not designed for cross-language use, the serialization format is closely tied to the way objects are represented inside the JVM. While implementing a JVM in Python is surely a fun exercise, it\'s probably not what you\'re looking for (-:</p>\n\n<p>There are other (data) serialization formats that are specifically designed to be language agnostic. They usually work by stripping the data formats down to the bare minimum (number, string, sequence, dictionary and that\'s it) and thus requiring a bit of work on both ends to represent a rich object as a graph of dumb data structures (and vice versa).</p>\n\n<p>Two examples are <a href="http://JSON.Org/" rel="nofollow noreferrer" title="JSON (JavaScript Object Notation)">JSON (JavaScript Object Notation)</a> and <a href="http://YAML.Org/" rel="nofollow noreferrer" title="YAML (YAML Ain&#39;t Markup Language)">YAML (YAML Ain\'t Markup Language)</a>.</p>\n\n<p><a href="http://ASN1.Elibel.Tm.Fr/en/" rel="nofollow noreferrer" title="ASN.1 (Abstract Syntax Notation One)">ASN.1 (Abstract Syntax Notation One)</a> is another data serialization format. Instead of dumbing the format down to a point where it can be easily understood, ASN.1 is self-describing, meaning all the information needed to decode a stream is encoded within the stream itself.</p>\n\n<p>And, of course, <a href="http://W3.Org/XML/" rel="nofollow noreferrer" title="XML (eXtensible Markup Language)">XML (eXtensible Markup Language)</a>, will work too, provided that it is not just used to provide textual representation of a "memory dump" of a Java object, but an actual abstract, language-agnostic encoding.</p>\n\n<p>So, to make a long story short: your best bet is to either try to coerce log4j into logging in one of the above-mentioned formats, replace log4j with something that does that or try to somehow intercept the objects before they are sent over the wire and convert them before leaving Javaland.</p>\n\n<p>Libraries that implement JSON, YAML, ASN.1 and XML are available for both Java and Python (and pretty much every programming language known to man).</p>\n'}, {'answer_id': 29582, 'author': 'jatanp', 'author_id': 959, 'author_profile': 'https://Stackoverflow.com/users/959', 'pm_score': 1, 'selected': False, 'text': "<p>Well I am not Python expert so I can't comment on how to solve your problem but if you have program in .NET you may use IKVM.NET to deserialize Java objects easily. I have experimented this by creating .NET Client for Log4J log messages written to Socket appender and it worked really well.</p>\n\n<p>I am sorry, if this answer does not make sense here.</p>\n"}, {'answer_id': 570540, 'author': 'penpen', 'author_id': 69022, 'author_profile': 'https://Stackoverflow.com/users/69022', 'pm_score': 0, 'selected': False, 'text': '<p>If you can have a JVM on the receiving side and the class definitions for the serialized data, and you only want to use Python and no other language, then you may use Jython:</p>\n\n<ul>\n<li>you would deserialize what you received using the correct Java methods</li>\n<li>and then you process what you get with you Python code</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/29505', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3431280/']