Skip to main content

Three Logs, Three Readers

Updated 8 min readMarkdown

The system keeps three logs. Two of them land in the same file, which is the first hint that the split was never about storage. It was about who reads the line.

Who reads this line?

The system log is the program talking about itself: which module, which line, what went wrong. A line reads JBook.ebook.txt.txt_reader:84, and the point of that name is that you know exactly where to look. It exists for the person debugging at midnight.

The operation log is a database table. One row per successful operation — who did what to which object — and nothing else: no IP, no User-Agent, no Referer. The "my operation history" page reads it back, one user at a time, newest first, through GET /api/operations. It exists for the user checking their own history.

The activity log is a text line per visit: who, from where, which page, what they did. It exists for the admin watching traffic and crawlers.

The same event lands in all three, in different shapes. Deleting a book writes an operation row (book_delete, the book's id and title), an activity line (from which IP, with which User-Agent), and whatever the code happened to log while doing it. A crawler fetching ten thousand pages writes zero operation rows — it never logs in and never "operates" — but ten thousand activity lines. That is the split: not what happened, but who needs to know and in what shape. Merge them and you either show the user raw IP and User-Agent lines, or make the admin query a table to count crawler hits.

A name is not a file

In Python's logging, the logger name is a signature, not a destination. The handler decides which file a line lands in. JBook learned the difference the expensive way: a grep in October 2026 found 222 direct logging.info(...) calls, which are the root logger in disguise, so every line came out as root:182 with no module to blame. Four batches replaced the 169 live ones with one logger per module; the only direct call left is inside third-party KindleUnpack code, deliberately untouched. The rest of the matches are commented-out lines, kept on purpose as a record of past performance tuning.

The payoff was demonstrated live, not argued. Set JBook.ebook to WARNING and the EPUB parser's debug chatter stops, while the rest of the system keeps talking. Before the fix that call did nothing at all — the output never passed through a JBook.ebook logger, so there was nothing to turn down.

The activity logger uses the same trick and stops one step short. It has its own name, JBook.activity, so a grep can pull its lines out of the shared file and its level can be raised alone — but it has no handler of its own, so it propagates up to root and lands in jbook-root.log next to the system lines. Splitting it into its own file later is a configuration change; activity_log.py would not need a single line changed. The name bought the control. The file was never the point.

The shared file itself does not rotate. The timed-rotation handler is commented out — a note in the config says rotation broke while the scheduler was running — so the file grows until someone empties it by hand with truncate -s 0, never rm: the running process still holds the file open, and deleting the name does not free the space.

Bots are graded by fame

Every request is sorted before it is written. A known crawler — Googlebot, bingbot, Baiduspider, and a dozen more names on a list — gets one short INFO line: event name, IP, SPIDER:Googlebot, URL. No identity, no extra fields. An unknown or suspicious one — curl, python-requests, anything with "bot" in the string, or no User-Agent at all — gets the same short line at DEBUG, which means it does not show up at the production INFO level. A human gets the full line: identity (a username, or the first eight characters of the anonymous ticket), URL, Referer, a parsed User-Agent summary, and whatever fields the call site passed.

The grading is about volume, not politeness. Unknown bots are the largest traffic and the least valuable; at INFO they would bury the humans. When someone is probing the site, the admin turns JBook.activity to DEBUG for a while and the invisible traffic appears. Known crawlers stay visible because search traffic is worth watching over months.

There is a manual marker too: ?b=9163, reserved for a sitemap channel that does not exist yet. The log already knows how to tag it via=sitemap, for the day the route appears.

One User-Agent, recorded twice

The User-Agent string is handled by two functions because it has two jobs. One matches the raw string against the bot patterns; the other parses it down to something like Chrome/120 (Windows 10) for display. If the raw string were replaced by the parsed one, the bot patterns would stop matching — they look for substrings of the raw text, version numbers, library names, the contact URL in parentheses, and a parsed summary keeps only browser family, major version and operating system. The substrings the patterns look for are not guaranteed to survive. So the raw string stays as the input to detection, truncated to 120 characters, and the display gets its own parsed copy, cached — there are only a few dozen real User-Agents in practice.

The one place this went wrong is instructive. A User-Agent the parser could not understand fell back to the raw string, which then got cut again to 48 characters — exactly the length of the display summary. The tail that was cut off, a domain name in one real case, was the part that identified who was scanning. Unparseable User-Agents are almost never browsers; every segment of them is a clue. Now the fallback returns the raw string at the full 120-character cap, and the 48-character limit applies only to summaries that parsed cleanly.

The visitor's IP is read the same careful way. It comes from the X-Real-IP header nginx adds, then the first hop of X-Forwarded-For, then the connection address as a last resort — read only at the moment of logging. The alternative, ProxyFix, would rewrite the request globally, and other code depends on the unrewritten values: whether the anonymous cookie gets the Secure flag hangs on it. Reading a header for one log line touches nothing else.

Logging everything is how you log nothing

The first version logged every page view with a before_request hook: if the browser said Sec-Fetch-Dest: document, write a line. It was switched off within a day. The header says nothing about the status code, so a 404 and a probe for /wp-admin got logged as real visits, with wp-admin recorded as an event name for a page that does not exist. And a bare scanner with no User-Agent slipped through the filter's fallback and got logged anyway. Page views are now buried by hand, one call per page — a single log_activity('home') in the bookshelf handler covers /, /home and /bookshelf — and the login, register and profile pages currently have no record at all, which is an honest gap rather than a noisy guess.

The same judgment runs through the rest of the list. Cover images are not logged — a list page fires one request per book. Preload calls are not logged — internal scheduling, not user intent. The reading-position endpoint, which the browser reports on every scroll, is not logged. The chapter-content endpoint is logged, on a standing instruction: log it first, comment it out when it gets noisy — one reader finishing a 300-chapter book writes 300 lines. And the two most sensitive call sites, selecting text to translate and asking the AI a question, record only character counts and language, never the text itself.

The fail-safe stops at the function door

log_activity promises never to throw: whatever goes wrong inside — encoding, IO, a broken handler — is swallowed, and the business code carries on as if the call were commented out. That promise held until a delete operation proved its boundary. A call site passed the facet's display name as name=..., colliding with log_activity's first parameter, which is also called name. Python raises TypeError while binding arguments, before the function body — and its try/except — ever runs. The delete had already succeeded in the database and in the operation log; the API still returned 500, and the user saw "delete failed" for a directory that was already gone. The fix was renaming the field to facet_name. Any entry point that takes **fields has the same trap: its first parameter's name is a reserved word, and no safety net inside the function can catch a mistake made at the call site.

Three logs cost three places to maintain. The alternative costs more: a single log where the person looking for a bug has to wade through crawler traffic, and the user checking their own history finds lines they were never meant to see.

About the author

  • Writing about ebook technology.

No comments yet. Be the first to comment.