This was a fun little build, but I wish I remembered to turn on my stream. It was kinda nice to just write some code, too ๐
<LogViewer>
takes a stream of server logs, or process logs, or any flat text file (let's be honest) and turns it into a searchable, filterable little thing. Type in the input box, find what you're looking for. Matched strings are highlighted so you know what you're doing.
This was fun. Writeup tomorrow ๐ค pic.twitter.com/40usRbykxR
โ Swizec Teller (@Swizec) July 18, 2018
The whole thing fits into 69 lines of beautifully rendered code and 1071 node_modules dependencies. You can see the code on GitHub โ๏ธ
Here's how it works ๐
<LogViewer>
is built out of 2 main components:
<LogViewer>
renders a Downshift component and munches a text file into a list of logs<LogRow>
takes care of individual lines and highlighting matched strings
LogViewer
We're using getDerivedStateFromProps
to take our logs
, which is a flat string, and turn it into an array of entries. Right now, that's just splitting by newlines, but we could perform contextual parsing and understand that a single log can span multiple lines.
Perhaps the parser function should come from props ๐ค
The render
method takes logs
from state and returns a <Downshift>
component. Downshift takes care of driving our input field and some other tidbits that are tedious to do ourselves.
Inside Downshift's render prop, we use matchSorter
to filter logs based on user input. I don't know what string matching algorithm matchSorter uses, but it's really fast.
A+ library ๐
Finally, we render a <div>
with an input
field controlled by Downshift, a p
with the count of matches, and a pre
with all our logs in a loop. We render each log entry with <LogRow>
, providing the current match
and log
to be rendered.
LogRow
The <LogRow>
component renders a string of text, potentially highlighting a part of it.
We convert the matched string to lower case. Gonna use it to decide which part of the log to highlight.
Then we split our log on a regex using the provided match.
The result is an array of log fragments like this
match = 'cat' log = 'my cat is grumpy'
chunks --> ['my ', 'cat', ' is grumpy']
When you split on regex and wrap your split point in parenthesis, ()
, it's preserved in your output. Perfect ๐ for our purposes and pretty fast too.
Rendering is a matter of looping through our chunks
array and returning either a flat string or the <Highlighted>
styled component.
The <Highlighted>
component is simplistic because I'm no designer.
const Highlight = styled.span color: red; background: yellow;
;
Red color, yellow background. High contrast.
Useful?
That was an MVP. Should I make it better? Should I open source it?
Anyone out there who'd use it? ๐คจ Ping me on Twitter.
Continue reading about Building a filterable log viewer with Downshift and match-sorter
Semantically similar articles hand-picked by GPT-4
- Build an animated pure SVG dynamic height accordion with React and D3
- Livecoding #25: Adding MobX to a vanilla React project
- Tiny React & D3 flamegraph tutorial
- Your first NextJS app โ CodeWithSwiz
- Custom react hooks โค๏ธ
Learned something new?
Read more Software Engineering Lessons from Production
I write articles with real insight into the career and skills of a modern software engineer. "Raw and honest from the heart!" as one reader described them. Fueled by lessons learned over 20 years of building production code for side-projects, small businesses, and hyper growth startups. Both successful and not.
Subscribe below ๐
Software Engineering Lessons from Production
Join Swizec's Newsletter and get insightful emails ๐ on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. ๐"
Have a burning question that you think I can answer? Hit me up on twitter and I'll do my best.
Who am I and who do I help? I'm Swizec Teller and I turn coders into engineers with "Raw and honest from the heart!" writing. No bullshit. Real insights into the career and skills of a modern software engineer.
Want to become a true senior engineer? Take ownership, have autonomy, and be a force multiplier on your team. The Senior Engineer Mindset ebook can help ๐ swizec.com/senior-mindset. These are the shifts in mindset that unlocked my career.
Curious about Serverless and the modern backend? Check out Serverless Handbook, for frontend engineers ๐ ServerlessHandbook.dev
Want to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz React components your whole team can understand with React for Data Visualization
Want to get my best emails on JavaScript, React, Serverless, Fullstack Web, or Indie Hacking? Check out swizec.com/collections
Did someone amazing share this letter with you? Wonderful! You can sign up for my weekly letters for software engineers on their path to greatness, here: swizec.com/blog
Want to brush up on your modern JavaScript syntax? Check out my interactive cheatsheet: es6cheatsheet.com
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are โค๏ธ