Skip to Content Treasure Data Logo Treasure Data Logo
  • Product
    • Agentic Experience Platform
      • Agentic Experience PlatformEnd-to-end AI-powered customer experiences.
      • Why Now?
      • Pricing
      • Trust & Security
    • Treasure AI Studio
      • Treasure AI SuitesOne workspace where teams turn customer intelligence into action.
      • Treasure Code
    • Treasure AI Suites
      • Treasure AI Suites Activate the right experience, in the right channel, every time.
      • Engagement Al Suite
      • Creative AI Suite
      • Personalization AI Suite
      • Paid Media AI Suite
      • Service AI Suite
    • Customer Data Platform
      • CDP Overview Your trusted foundation for first-party customer intelligence.
      • Hybrid Architecture
      • Agent Hub
      • Integrations
  • Solutions
    • Industries
      • Automotive
      • CPG
      • Entertainment & Media
      • Financial Services
      • Healthcare
      • Retail
      • Technology
      • Travel & Hospitality
  • Customers
  • Resources
    • Explore
      • Resource Library
      • Case Studies
      • Blog
      • Documentation
      • Training
      • Events
      • Webinars
    • Get Started
      • Demo Experience Treasure Data with an expert-led walkthrough.
      • Trade-Up Program Replace your CEP, CDP, or ESP. Save big with special incentives.
  • Company
    • Company
      • About Us
      • Careers
      • Partners
      • News
      • Contact Us
      • Terms
Login
Get a demo

Treasure Data is now Treasure AI: Co-founder & CEO announcement ->

  • Menu Item 1
    • Sub-menu Item 1
      • Another Item
    • Sub-menu Item 2
  • Menu Item 2
    • Yet Another Item
  • Menu Item 3
  • Menu Item 4
Blog
    • CDP
    • Customer Data Strategy
    • AI & Machine Learning
    • CDP Use Cases
    • Marketing
    • Treasure Data CDP
    • Data Privacy & Security
    • Partners
    • AI & Marketing
    • Company News
    • Data Strategy
    • Customer Service
    • Privacy & Security
April 26, 2012

Log Everything as JSON. Make Your Life Easier

Ron Zvagelsky Ron Zvagelsky
  • Treasure Data CDP

The Story of an Engineer.

Here is an anecdote. I am sure some of you have had a similar experience.

  • Alex, an engineer, logs all kinds of events. Since he is the primary consumer of the log, the format is optimized for human-readability. One canonical example is Apache logs:10.0.1.22 - - [15/Oct/2010:11:46:46 -0700] "GET /favicon.ico HTTP/1.1" 404... 10.0.1.22 - - [15/Oct/2010:11:46:58 -0700] "GET / HTTP/1.1" 200...This looks great. Time stamp, URL, HTTP status code...each line gives Alex a lot of information to work with if the service is having issues.
  • Bob, a business analyst, asks Alex for the number of daily unique users. Alex writes a parser for the Apache log and crontabs the script. He also builds a little Web interface so that his colleague can query the parsed data on his own. Bob finds the interface super useful.
  • Bob comes back a few weeks later and complains that the web interface is broken. Alex scratches his head and takes a look at the logs, only to realize that someone added an extra field in each line, breaking his custom parser. He pushes the change and tells Bob that everything is okay again. Instead of writing a new feature, Alex has to go back and has to fill back the missing data.
  • Every 3 weeks or so, repeat Step 3.

What is Wrong With This?

The takeaway lesson of the above story is twofold:  (1) logs are not just for humans to read and (2) logs change. (1) Logs are not just for humans. As  Paul Querna points out, the primary consumer of logs are shifting from humans to computers. This means log formats should have a well-defined structure that can be parsed easily and robustly. (2) Logs change. If the logs never changed, writing a custom parser might not be too terrible. The engineer would write it once and be done. But in reality, logs change. Every time you add a feature, you start logging more data, and as you add more data, the printf-style format inevitably changes. This implies that the custom parser has to be updated constantly, consuming valuable development time.

Enter JSON!

Here is a suggestion: Start logging your data as JSON. JSON has a couple of advantages over other "structures".

  • Widely adopted. Most engineers know what JSON is, and there is a JSON library for every language imaginable. This means there is little overhead to parse logs.
  • Readable. Readability counts because engineers have to jump in and read the logs if there is a problem. JSON is text-based (as opposed to binary-based) and its format is a subset of JavaScript object literal (which most engineers are familiar with). In fact, properly formatted JSON is easier to read than logs formatted in ad hoc ways.

An Example of JSON-based Logging: Fluentd

We've already  talked about Fluentd in this blog, so I won't bother you with the details. It's a logging daemon that can talk to a variety of services (ex:  MongoDB,  Scribe, etc.) One of the key features of Fluentd is that  everything is logged as JSON. Here is a little code snippet that logs data to Fluentd from Ruby.

require 'fluent-logger'
 # some code in between
 log = Fluent::Logger::FluentLogger.new(nil, :host => 'localhost', :port=>24224)
 log.post('myapp.access', {"user-agent" => user_agent})
 
Now, suppose you wanted to start logging the referrer URL in addition to user agent. You just need to update the Ruby hash that corresponds to JSON.
require 'fluent-logger'
 # some code in between
 log = Fluent::Logger::FluentLogger.new(nil, :host => 'localhost', :port=>24224)
 log.post('myapp.access', {"user-agent" => user_agent, "referrer" => referrer_url}) # Added a field!
 
That's the only change you need to make. All the existing scripts work as before, since all we did was adding a new field to the existing JSON. In contrast, imagine you were logging the same data in a printf-inspired format. Your code initially looks like this:
log = CustomLogger.new
 #some code in between
 log.post("web.access", "user-agent: #{user_agent} blah blah")
 
When you decide to log the referrer URL, you update it to:
log = CustomLogger.new
 #some code in between
 log.post("web.access", "user-agent: #{user_agent} blah blah referrer: #{referrer_url}")
 

 

Now, most likely your old parser is broken, and you have to go and update your regex and whatnot. We are biased towards Fluentd because we wrote it ourselves. But regardless of what software/framework you choose for logging, you should start logging everything as JSON right away.

Topics Covered

  • Treasure Data CDP

Share this story:


Subscribe
Get Treasure Data blogs, news, use cases, and platform capabilities:

Thank you for subscribing to our blog!

Recent Posts

14 min read
Why We're Becoming Treasure AI
Customer Data Strategy 8 min read
CDP vs MDM: What Marketers Need to Know About Unified Customer Data
Treasure Data Logo Symbol

+1 866.899.5386 (US)
+1 650.772.4500 (Non-US)

  • Product
    • Agentic Experience Platform
      • Agentic Experience Platform End-to-end AI-powered customer experiences.
      • Pricing
      • Why Now?
      • Trust & Security
    • Treasure AI Studio
      • Treasure AI Studio One workspace where teams turn customer intelligence into action.
      • Treasure Code
    • Treasure AI Suites
      • Treasure AI Suites Activate the right experience, in the right channel, every time.
      • Engagement AI Suite
      • Creative AI Suite
      • Personalization AI Suite
      • Paid Media AI Suite
      • Service AI Suite
    • Customer Data Platform
      • CDP Overview Your trusted foundation for first-party customer intelligence.
      • Composable Architecture
      • Agent Hub
      • Integrations
      • Personalization AI Suite
  • Solutions
    • Industries
      • Automotive
      • CPG
      • Entertainment & Media
      • Financial Services
      • Healthcare
      • Retail
      • Technology
      • Travel & Hospitality
  • Resources
    • Explore
      • Resource Library
      • Case Studies
      • Blog
      • Pricing
      • Documentation
      • Training
      • Events
      • Webinars
    • Get Started
      • Demo
      • AI Workshop
      • Fast Proof of Concept
      • RFP Template
      • Trade-Up Program
      • Value Calculator
  • Company
    • Company
      • About Us
      • Customers
      • Partners
      • Careers
      • News
      • Contact Us
      • Terms
  • Get a demo
  • Terms & Conditions
  • Privacy Statement
  • Cookie Policy
  • Privacy Hub
  • Trademarks
  • Modern Slavery Statement
  • Your Privacy Choices
©2026 Treasure Data, Inc. (or its affiliates) All rights reserved.