Django Logging

Posted on

Logging is mostly about trade off between easy of troubleshooting and the overhead. Log too much makes the app slow, and noisy log output is also hard to trace. Log too little won’t help at all. Many of the points below are from the Python logging cookbook.

https://docs.python.org/3/howto/logging-cookbook.html

1/ Python’s logging module

Django make use of Python’s built-in logging module. The way you use it in traditional Python app is:

import logging

logging.basicConfig(level=..., stream=...)

logging.debug('Fetching data from %s', url)
logging.info(...)
logging.error(..., exc_info=True)
logging.trace(...)

You can also use logger = logging.getLogger(...), but it’s not necessary.

Django already have logging module setup, so you don’t need to call basicConfig() anymore, just import logging and uses it.

Log level:

  • Error: Should be use for fatal errors and unrecoverable failure. Good places to use error log:
  • wherever you have a try-except block, for example: failed call to external …
Read more »

Using Selda with Servant

Posted on

I came to Haskell late last year, and of course nowadays we’re mostly working with APIs and databases. In the Haskell ecosystem, I think the API part has already been figured out. We have Servant. The first time I worked with it I talked to myself: this is how you should be building API, request in, record out, nothing else. With Servant you will not have to worry about mundane tasks like adding correct header, method checking and content negotiation, .etc, just focus on writing the logic that really matter.

The SQL part though, is still messy. There’s Persistent, which is kind of like ActiveRecord, in the sense that you have little control over your queries, people use it with a separate query builder (Esqueleto) which is not ideal. There’s also postgresql-simple which seems loved by everyone, but it is pretty low level and certainly not type safe. I’m coming to Haskell for safety, and if it is not guaranteed I would just come back to Go. Until I met Selda, it feels just right. In my opinion it is at the …

Read more »

Use TEXT instead of VARCHAR with PostgreSQL

Posted on

In recent project I’m migrating from postgresql-simple to Selda. I’m happy with postgresql-simple, but really wanted some extra type-safety, and a much more readable query syntax. Along the way I was hit by an error:

BUG: result with unknown type oid: Oid 1043
CallStack (from HasCallStack):
  error, called at src/Database/Selda/PostgreSQL/Encoding.hs:72:24 in selda-postgresql-0.1.7.2-GpYhYBkrTMb6s8gal7hgyK:Database.Selda.PostgreSQL.Encoding

Turn out the column type I was using is VARCHAR (Oid 1043) is not supported by Selda. I’m surprised because VARCHAR is the first thing I learned when I was studying SQL. I looked at the source code and the only textual data type supported is TEXT (Oid 25). Of course before submitting a bug report I searched around and, to my surprise TEXT is the right choice, from the PostgreSQL documentation:

There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few ext…

Read more »

Extract components painlessly from Tachyons with Less

Posted on

With the rising popularity of utilities CSS libraries (or functional CSS, it you are into fancy names) such as Tachyons, Basscss and the likes, there some emerging question going with them:

CSS helper libraries are good for rapid prototyping, there’s no deny of that

What’s the point of CSS helper libraries? Are they just inline style

TBH inline style is not essentially a bad thing. I mean Basecamp lead designer use it, we do it all the time with React components, and frankly that’s what we have been told not to do since day one. The real reason for that is it’s not maintainable, has high specificity and hard to override. Tachyons helps with 1/ consistent specificity 2/ consistent measure system - vertical rhythm. Once you have your design it’s perfectly fine to extract to components and style guide

Component extraction: looking at Tailwind, and the article,

Here’s how I’ve been doing it, with Less…

Read more »

Ractive Component Brunch Plugin

Posted on

So I made a Brunch plugin to compile Ractive’s single file component format. You can take a look at it here: https://github.com/tungd/ractive-component-brunch.

React is probably the best thing happened to the frontend world in many years. React has formalized the concept of “reusable, isolated frontend component” and revolutionized how we do frontend development. In my mind I often compare it with the introduction of HTML5; not the markup, but the API part that enables developer to build more ambitious apps. I has been a React advocate for years, but we have to acknowledge that the ecosystem is quite heavy in term of setup and learning curve.

React, being just a view layer its comes with all the complexity of picking a data layer (Redux) and a build system for advanced stuffs that it requires (Webpack, Babel). I don’t build SPA all days, sometimes I just build widget plugged into someone else’s frontend so their customer can use our service seamlessly. I’ve be…

Read more »

Build a web application in Haskell

Posted on

This week’s Haskell Weekly (issue 82) include an interesting slide of one developer sharing his experience rewriting a Rails project using Haskell. You can read the slide here:

[Slides] Joy & frustration of putting 34,000 lines of Haskell into production

I’m new to Haskell, however I have plenty experience building apps in Django and Rails, and all of the slides’ issue is very relevant in building production app. The slide make me worried I decide to start building a small, yet product-ready app to explore the Haskell eco system for my self.

In this project we’re going to build a Twitter clone. I will also focus on making it as close to production ready as possible, in the same spirit of the Rails tutorial https://www.railstutorial.org/book. The app will include

  • Database model definition and querying
  • Database migration
  • Deployment, with continuous integration and…
Read more »

My first Haskell package: time-locale-vietnamese

Posted on

I’ve been learning Haskell for a while, today I release my very first Haskell package: time-locale-vietnamese. Just a simple TimeLocale to support formatting time in Vietnamese. Here is a quick example:

import Data.Time
import Data.Time.Locale.Vietnamese

main = do
  now <- getCurrentTime
  print $ formatTime vietnameseTimeLocale "%A, %d %B %Y" now

I’ve packages published to various repository before, with Hex for Elixir, and NPM for JavaScript. Overall the process for Haskell is pretty simple. In the beginning all I found is talking about Cabal sdist; I could not find any document about how to do it with Stack. At last it is just stack upload but the document could be clearer.

The package only have vietnameseTimeLocale for now, but I plan to add common time format phrases late. Hope this is useful.…

Read more »

WooCommerce custom order state

Posted on

WooCommerce custom order state

Before diving into the code, consider this:

No code is better than no code.

So what is a good use-case for custom WooCommerce order state? More often than not, the product you sell will involve some kind of operations that you want to track transparently from customer, for example ordering the materials from a supplier, or send it off to a custom shop. WooCommerce is so matured it is possible to implement complex order processing pipeline just utilizing its note system. I mean obviously you can add a note every time you need to order from a supplier. The down side of this is the staff might forgot the details, or the note can get inconsistent over time. Moreover since the note is just text it is hard to keep track and filter the order of various state. Custom order state can really stream line the whole operation.

Adding custom order state to WooCommerce

Let’s say you’re selling leather goods, and allow customers to have their name engrave into the tag. You have a part…

Read more »