Although it's a very small project, I'm excited to release my first open source Node.js package today. Sitka is an extremely lightweight but powerful logger that's great for modern cloud/serverless applications. It can be installed using the Sitka NPM package and the source code for Sitka is on GitHub. Yes, there were already tons of loggers to choose from. When I browsed through them, most seemed to fall into 2 categories: many felt too rigid while others were "kitchen sink" projects trying to do far too much. A logger is an essential part of every application, but it should be simple to use and stay out of your way. As a minimalist, I want something flexible without a lot of overhead. Sitka stands on its own with less than 250 lines of code and without importing any dependencies—much less half a dozen.
Flexible Logger Configuration
There's one core feature for a logger: write text to a target location in a predictable format while ignoring messages deemed unimportant in a given environment. Thus, the common variants for logger instances are:
- minimum level of entries to be written
- format of log entries
- log location/destination
Sitka allows configuration of log level and format at both a global and instance level. You can set each using environment properties or the constructor. In production environments, many high traffic sites only log exceptions, errors, and warnings by default. The ability to increase the log level for a specific logger instance at runtime means you can zero in on the component having trouble without generating gigs of log data. To change the log destination, you can simply redirect stdout/stderr or provide a custom function that sends the data to any store using the transport of your choosing. Since each of these common variants are configurable, Sitka really should work for almost everyone. For details and code examples, refer to the Sitka documentation on GitHub.
The Power of Context
When you're debugging an application, what makes a log message useful? Think about why stack traces are so helpful: it's due to the context they include from the time of the exception. Productive log messages expose context that provides insight into the point-in-time state of the execution environment. Sitka makes it easier to surface those details by including special variables in the log format and/or individual messages. For example, an environment variable in the log format can reveal which container (or server if that's the life you're living) is seeing frequent errors so you can remove it from the cluster.
Besides environment variables, you can also set custom context variables in code (globally and at the instance level) to make them available in log messages. This level of control can go to whatever depth you desire, which makes it easier to write consistently useful log messages. For example, if your User class has an ID property and you want to know what account encountered any given issue, you could set the ID in the logger's context and include it in the format for that logger instance. Then every log entry from your User class would include the ID without explicitly adding it to the log messages. The Sitka documentation on GitHub includes an advanced example showing how to leverage this functionality in an AWS Lambda environment.
Origin of the "Sitka" Name
Logs come from trees, and the Sitka spruce is known for its high strength relative to its light weight. Its wood was commonly used to build early airplanes including the Wright brothers' first successful heavier-than-air powered aircraft and is still used to build many string instruments and other products. Sitka aims to remain a lightweight but powerful logger that feels at home in the cloud.
Let's face it: not many people get excited about a logger. Frankly, I'm surprised you read this whole post. You should try Sitka in your next project and let me know what you think in the comments below or using my contact page.