<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Charly Castes on Medium]]></title>
        <description><![CDATA[Stories by Charly Castes on Medium]]></description>
        <link>https://medium.com/@CharlyCastes?source=rss-5c15123ef41d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*aMgOE9xe0LdYkgK4Pp3mSQ.jpeg</url>
            <title>Stories by Charly Castes on Medium</title>
            <link>https://medium.com/@CharlyCastes?source=rss-5c15123ef41d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 23:46:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@CharlyCastes/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Fitting automaton for unsupervised log parsing]]></title>
            <link>https://medium.com/jokoapp/fitting-automaton-for-unsupervised-log-parsing-d04400de206a?source=rss-5c15123ef41d------2</link>
            <guid isPermaLink="false">https://medium.com/p/d04400de206a</guid>
            <category><![CDATA[finite-state-machine]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[parsing]]></category>
            <dc:creator><![CDATA[Charly Castes]]></dc:creator>
            <pubDate>Mon, 19 Aug 2019 08:01:01 GMT</pubDate>
            <atom:updated>2019-08-19T08:01:01.876Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HHghnBj7kC6BTn3D0rSArw.jpeg" /></figure><p>Imagine for a second that you’re a system admin, you’re responsible for managing your company’s servers that run YourAmazingService. Unfortunately at some point YourAmazingService crashes and you have to find out why, so you roll up your sleeves and start digging through the server’s syslog. Fortunately there are lot of tools out there to help you have the job done, for instance let’s unleash your secret weapon: grep.</p><p>With grep ERROR you&#39;ll get all the lines containing the word &quot;ERROR&quot; in your logs, pretty useful to track any of those error right? Ok maybe you&#39;ll have to try a few other keywords such as &quot;Failure&quot;, &quot;Stop&quot;, &quot;Does not respond&quot; but in the end you&#39;ll find the very line you&#39;re looking for.</p><p>Some time passes and eventually you get bored of searching through logs, so you decide to set up a log management tool to help you in your daily job. Again there are many choices open to us, especially to manage logs from well known services such as Mysql or Nginx to quote a few. However YourAmazingService is using its own log format, the software engineers created the following logging function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54260c102acc16cab77d40b887cc44d6/href">https://medium.com/media/54260c102acc16cab77d40b887cc44d6/href</a></iframe><p>For instance, some logs of YourAmazingService may look like this:</p><pre>[YourAmazingService]: 02/08/2019 request GET status SUCCESS 200<br>[YourAmazingService]: 07/11/2019 request PUT status SUCCESS 200<br>[YourAmazingService]: 27/03/2019 request POST status ERROR 500</pre><p>You want to plot the number of errors over time, just to be sure everything is OK, so you have to parse the logs yourself. Because you’ve looked into the code, you know how the logs are created, they are quite easy to parse aren’t they? First you retrieve the status, that’s the word between “status” and some final three digits numbers, the regular expression status\\s(.+)\\s\\d+$ does exactly that. Ok now you have to deal with the date, it is easy to find because of the forward slashes / , you can use this regular expression: (\\d+)\\/(\\d+)\\/(\\d+) that captures any group of digits followed by a slash (you need to escape it with a back slash, that is why you use \\/) itself followed by a group of digits with another forward slash and a final group of digits.</p><p>And voilà !</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/0*ZFVXuyo9MT9GtA6o" /></figure><p>Now all your colleagues are jealous because you have developed an amazing tool to monitor YourAmazingService in real time by reading the logs, so they ask you to do the same for every other services used by the company. This time this will not be that simple, because YourAmazingCompany is using hundred of distinct services, which all have their own log format, some teams often add or remove services and, as time passes, the log format is evolving. What a pain, you certainly don’t want to maintain a list of 500 regexes (regular expressions) by yourself, What if we could automatically generate the regexes to parse all these different log formats?</p><p>At Joko we are basically facing the same issue, except that the logs aren’t generated by our servers but by our users’ banks. Joko turns your debit or credit card into a loyalty card so that you earn points each time you spend at one of your five favorite brands. Thus each time you pay for something we have to check from the corresponding bank log which merchant it is and grant you points if it’s one of your favorite five. Here is what a transaction log looks like:</p><p>CB MC DONALDS 30/07 PARIS 17 75 CB N° 3081 322XX XXXX 3214</p><p>If McDonald’s is one of your favorite brands in Joko, we’ll give you some points for this transaction.</p><p>We are currently dealing with more than 50 banks in France, many of them often use more than one template (as if YourAmazingService produced multiple types of logs, let’s say one for HTTP request and one for hard drive access) and as we plan to get to other countries this number will grow higher and higher in the future.</p><p>To deal with this issue of scalability and maintenance we came up with an algorithm that learns a parser tailored for a given bank from examples of logs, with no human intervention at all. But before telling you how it works, let’s introduce <strong>finite state machines</strong>.</p><h3>Finite State Machines</h3><p>We do know that the logs have been generated by some sort of logging function that uses a predefined template, a convenient way to represent this generative process is to use a finite state machine (FSM), the one below is equivalent to the logging function used by YourAmazingService.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/0*AMmK4kOhkFr7KhUd" /></figure><p>To read it start by the upper left state with the incoming arrow, the <em>start state</em>, and walk through the transitions until you reach the <em>stop state</em>, which is the one with the inner circle.</p><p>Let’s check if the sentence [YourAmazingService]: 02/08/2019 request GET status SUCCESS 200 is matched by the FSM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*IvY1ZXr-JerwevXJ" /></figure><p>Here we are on the start state, let’s look at the first word: [YourAmazingService]:, it matches the outgoing transition and thus we can go forward. Had it been a different word, the journey would have stopped here, if this happens we say that the FSM did not match the sentence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/0*naX1y_Plscys0a-c" /></figure><p>The next word (words are separated by spaces here) is 02/08/2019, it matches again the outgoing edges (to keep the same notation as above {} may hold any value, thus {}/{}/{} matched 02/08/2019)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/903/0*p2Y3XbHH7aoBxCo6" /></figure><p>And so on, request matches too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/0*ib0MWbVSt7nEdLDv" /></figure><p>Let’s skip some steps, eventually we reach the end:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/0*XB7QtAhwxXjhrag8" /></figure><p>There is no word left and we are now on the stop state, in this case we said that the FSM matches our sentence.</p><p>From a more formal point of view, finite state machines defines what we call a language, this language is the set of every sentences that matches the FSM. The one above defines a language that contains all the logs than can ever be generated by YourAmazingService, in other words every logs issued from YourAmazingService will match this FSM. In fact this FSM is even more general, because for this example we haven’t set any constraint on variables, for instance a stricter version should specify the date as a two digit number, a slash, a two digit number, another slash and a four digit number.</p><p>Using FSM is more convenient than “templates” because in the end it is just a graph, and it is much easier to manipulate than a raw string. With this in mind let’s go back to our problem that we can now phrase like this: how to learn FSM automatically from a list of examples?</p><h3>Learning bank log parsers</h3><p>As a system admin that monitor some servers you only about the status and the date, everything else is noise you need to filter. At Joko we are interested in which merchant appears in each log. For instance, take a look at the following logs:</p><pre>PAIEMENT PSC 1008 LYON LE BON TEMPS CARTE 16894975<br>PAIEMENT PSC 0218 PARIS PHIE ITALI CARTE 06894975<br>PAIEMENT PSC 0208 BORDEAUX PISCINE CARTE 86894975<br>NETFLIX CARTE 17218847 PAIEMENT CB 0602 LONDON<br>CARREFOUR CARTE 85875128 PAIEMENT CB 2807 MARSEILLE</pre><p>Those are real logs (with fake numbers of course), what matters to us is this:</p><pre>LYON LE BON TEMPS<br>PARIS PHIE ITALI<br>BORDEAUX PISCINE<br>NETFLIX LONDON<br>CARREFOUR MARSEILLE</pre><p>In fact if we look at the following FSM, which defines a language that contains all the five logs (and probably others that would be generated by this banks), the interesting words are those on transition with variable value {}. They are highlighted in red on this figure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/0*fLx6vNTXDiRAthZ5" /></figure><p>A few quick notes about the finite state machine above:</p><ul><li>We use {4d} to describe any 4 digit word, an {8d} for 8 digit words.</li><li>The three first of our five example logs are matched by the upper path, the two last by the lower one.</li><li>This FSM happens to be a <em>nondeterministic finite automaton</em>. Automaton because a FSM is an automaton, and nondeterministic because if you stand on the start state and encounter the word “PAIEMENT” you have the choice between going up or going down, and when you have to choose it is not deterministic.</li></ul><p>Back to our business, if we are able to automatically generate a relevant FSM for each bank it’s easy to parse the logs and filter only what matters: the merchant itself. And the better we are at retrieving the merchant, the more points our users will get!</p><p>To sum up: we have many, many logs for each bank and we want to build a relevant FSM automatically. We will reuse the same logs to illustrate the process.</p><pre>PAIEMENT PSC 1008 LYON LE BON TEMPS CARTE 16894975<br>PAIEMENT PSC 0218 PARIS PHIE ITALI CARTE 06894975<br>PAIEMENT PSC 0208 BORDEAUX PISCINE CARTE 86894975<br>NETFLIX CARTE 17218847 PAIEMENT CB 0602 LONDON<br>CARREFOUR CARTE 85875128 PAIEMENT CB 2807 MARSEILLE</pre><p>We will iterate on our FSM to make it better and better, but we need to start somewhere right? Here are the two options offered to us:</p><ol><li>The ultimate machine:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/0*PsgfRLkWMM24csHt" /></figure><p>Because in our convention {} can match anything, including multiple words, this FSM define the ultimate language that contains any sentence you can ever think of, and even some you can&#39;t ! You will probably agree that it isn&#39;t really interesting though...</p><ol><li>The over-specialized machine:</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/0*awxcpyL5jCWr2DoW" /></figure><p>This FSM defines the language containing exactly the five logs of our training set, no less no more. It’s good that it won’t accept logs from other banks that use distinct templates, however it will not accept most of the logs from the same bank either because the merchant, card number or location will not match.</p><p>It seems easier to go from an over specialized FSM to a just enough specialized one than starting from scratch, so let’s do this.</p><p>First, a small step of pre-processing: we don’t really care about numbers, in fact for us “0218” and “1008” are quite the same thing, all that matter is that they are two 4-digits numbers. Se we replace numbers by {4d} or {8d} in our FSM:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/983/0*A71zxyr0LT_jJeQR" /></figure><p>At least with this change users with any cards that bought something to one of these five merchants will be matched by the FSM, that’s better but not enough.</p><p>Now, how can we know which transitions of the hidden finite state machine are fixed which ones hold variables? Well if something is written on a fixed transition it should appear very often, shouldn’t it? So let’s count the number of appearance of each words that appear in, let’s say more than 20 % of the logs, that is more than once in this example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/0*3TU_rdtDs4ntGBlB" /></figure><p>CARTE appear in each logs, so it&#39;s probably written on a transition of the true FSM used by the bank.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/0*QV0vE6AwhkVynBqy" /></figure><p>There is a good chance that in fact, in the real, hidden automaton all these transitions lead to the same state, so let’s reduce our huge automaton by merging all the blue states together:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/976/0*q_iZdXMEejd_fOC_" /></figure><p>Apart from being much uglier and harder to draw, we got rid of 4 states. Let’s continue with the next most common word: PAIEMENT</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/0*JLm5YhPoOtFgQWKc" /></figure><p>Wait there is a catch here, as you can see there is two groups of transitions with PAIEMENT, one before the merged node and one after. We can&#39;t merge all those highlighted nodes together otherwise we create a cycle, and we don&#39;t want loops because we assume that the real FSM is an acyclic automaton.</p><p>The problem of finding the biggest group to merge can be framed as follows: we have a set of nodes and a list of constraints (i.e. node X can’t be in the same group as node Y), what is the biggest set of nodes we can build such that no constraint forbids two nodes of this group to coexist?</p><p>In our particular setting node X can’t be in the same group as node Y if node X is an ancestor of Y. Here all the red nodes are ancestors of the blue nodes, so they can’t be grouped together.</p><p>This seems quite easy to solve right? In fact even if it is quite obvious on this example, it is not in general, and it belongs to a class of hard problems called NP-Complete problems. In other words it is so hard that if you find an efficient way to solve it you’ll be awarded one million dollars. If you’re interested in algorithmics you can have a look at the maximal clique problem, one of Karp’s 21 NP-Complete problems which is equivalent to this one. However it is quite simple to find a good solution (but not always optimal) with a fast algorithm, which is what we are doing to ensure a manageable runtime even when dealing with thousands of logs.</p><p>Anyway, the red group is obviously the largest one so let’s merge all those red nodes together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/971/0*hcd6srgkwDQwdUq5" /></figure><p>Now PAIEMENT appears only three times, we will go back to them later but for now there are other words that appear 5 times. The next word on our list is {4d}, so let&#39;s go !</p><p>Well… In fact let’s rather jump directly to the point where all the most frequent words are merged, now our FSM looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/964/0*6mm9obX8UXOhFH27" /></figure><p>Ok that’s cleaner, but there is still a lot of transitions. In blue are highlighted the transitions that have not been merged because they appear with a low frequency. We strongly suspect that those are in fact variables in the bank’s hidden FSM, so let’s replace them with the placeholder for variable values: {}</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/0*15yZAm5oMgC67DEP" /></figure><p>Because {} can match any number of words, having {} -&gt; {} mean two words or more. We can safely clean any chain of {} and replace it by a single transition.</p><p>By the way we can simplify this FSM a little bit: some path appear multiple times, such as {} -&gt; CARTE between the two green nodes, so we can get rid of the duplicates too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/0*X-YSCflJKQ3oZKSc" /></figure><p>It looks good, doesn’t it? We can clearly distinguish the blue path that correspond to the first template of logs and the green path for the second template. In fact it’s almost a perfect match, except for one node. Here is the target with the same colors so that you can compare the two:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/937/0*0H4-h3VibKpTS0PJ" /></figure><p>Let’s take a step back and think about what really happened here. Remember the ultimate-machine and the over-specialized FSM we started from ? Those define languages as we discussed before: the ultimate-machine defines a language that contains any sentence while the over-specialized FSM’s language contains exactly the logs from the training set. However our target is none of these two languages, it is the language that contains every logs than can be generated by the bank, it is obviously smaller than the ultimate language but much bigger than the over-specialized one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/809/0*tk70Qe4ig-adNw6C" /></figure><p>If you take a closer look to each operation we’ve done on the FSM, you’ll find out that each of those does only broaden the language generated by the FSM. For instance when merging a node new paths become available and when replacing a word by the generic value {} it can now take an infinite number of values. In the end we get something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/0*rqV5D-qZzEYNOY3l" /></figure><p>Under reasonable assumptions, such that a word sampled from a variable transition does not appear more than a given frequency threshold <em>t</em> and that we have at least one log for each possible path on the hidden bank’s FSM, we are granted that our generated language contains the bank’s hidden language.</p><p>However the outer magenta circle may be too big, for instance in our generated FSM you can start on the blue path of the FSM and end your journey on the green one. In practice if a log is generated by a given hidden FSM’s path, say the blue one, it will always match its equivalent on the generated FSM (the other blue one), but sometimes because our automaton is nondeterministic and covers a broader language it will also match another path. Here is an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/0*YlDbllTZ5REUhBlx" /></figure><p>This sentence can be matched by two paths, the full blue path or a mix between the green and blue one. Here is what we extract from each of those path (i.e. what words fall on a {} transition):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/0*oJ4bj9jhfOhAAJkG" /></figure><p>The blue one is a perfect shot, only valuable informations. The second one is very noisy and will produce a low quality feature for the next input of our processing pipeline.</p><p>Here is what happens, when we stand on the marked node below we have two choice: going up or going down.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/955/0*FaaJx53j2KI_kn1k" /></figure><p>In finite state machine there is no such thing as a memory, when we stand on this mark we do not know if we got there from the blue or green path. But because in our training set we’ve seen 3 examples of logs that were merged into the blue path, and two that were merged into the green path but none that were merged in a mix of blue-green path, we are intimately convinced that taking a half blue half green path is a bad idea.</p><p>We can formalize this a little bit and compute probabilities on each transition of the graph knowing the last 3 states for instance. This is what we call a Markov chain: at one point the next transition probabilities depend only on these 3 last states.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/0*lwuvP7y9u7NQTUXo" /></figure><p>With this trick when two or more paths match we can select the most probable one, thus solving the issue of non-determinism and increasing the quality of the data extraction.</p><h3>Conclusion</h3><p>We’ve gone a long way through our journey among finite state machines and algorithms, but before leaving you to your daily occupations we’ll share some example of real FSM and their results: here is one trained on 1000 bank logs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/0*QhHgLiCUM4KOgh_B" /></figure><p>You can find some of the logs below. We apply some small preprocessing steps such as lowering text and replacing / by space.</p><pre>CB ESSO FOCH 05/06 SAINT MAUR DE 94 CB N° 3451 22XX XXXX 7643<br>CB SM CASINO CS370 01/10 PARIS 75 CB N° 4533 21XX XXXX 6380<br>CB RATP 02/02 PARIS 75 CB N° 4561 22XX XXXX 5324<br>CB ITUNES.COM/BILL 19/02 IE ITUNES.COM CB N° 4323 61XX XXXX 3564<br>SNCF INTERNET<br>CB PICARD SA 555 20/10 MENTON 06 CB N° 5362 30XX XXXX 6423</pre><p>And here is what the FSM extracted: the merchant and it’s location, it’s much easier to process now.</p><pre>esso foch saint maur de<br>sm casino cs370 paris<br>ratp paris<br>itunes.com bill ie itunes.com<br>sncf internet<br>picard sa 555 menton</pre><p>To conclude let’s sum up the strength of this technique:</p><ul><li>You can learn a parser for any log generator: if you want to monitor the number of errors against each type of request inside your huge company data center, you can automatically create a parser tailored for each of the target softwares regardless of their logs format.</li><li>There is no human work needed, it is unsupervised. This task can also be tackled by machine learning methods such as Named Entity Recognition (which aims at finding the role of words in a sentence) but those require a lot of labeled data, thousands of sentences, and we mean thousands of sentences per log generator: this would be completely humanly un-tractable and not scalable at all.</li></ul><p>At Joko this is a very powerful preprocessing step that sets up the stage for the rock solid learning algorithms that help us rewarding you every day!</p><p>If you’re interested, we published an open source demo here: <a href="https://github.com/hellojoko/autoparse">https://github.com/hellojoko/autoparse</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d04400de206a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/jokoapp/fitting-automaton-for-unsupervised-log-parsing-d04400de206a">Fitting automaton for unsupervised log parsing</a> was originally published in <a href="https://medium.com/jokoapp">Joko Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>