Score:0

Is it possible to pad columns in nginx access/error logs?

ml flag

I've recently enabled ipv6 on my site, and while I don't often have a need to manually look at an access_log file, on the occasion that I do it's now very jarring that my first (IP) column is sometimes 7 characters long and sometimes 39 characters.

Is there any way of padding the first column to be always (say) 39 characters wide?

I've googled and looked at the nginx docs and I can't find anything, but figured someone on here might know a trick...

I assume if nothing else it'll be possible with map somehow, but I only know to use regexp with that and can't think of a way to play with length or padding...

Score:0
ml flag

OK, since writing it I've come up with my best (read: only) solution:

map $remote_addr $padded_ip { "~^(.{39})" "$1";
                              "~^(.{38})" "$1 ";
                              "~^(.{37})" "$1  ";
                              "~^(.{36})" "$1   ";
                              "~^(.{35})" "$1    ";
                              "~^(.{34})" "$1     ";
                              "~^(.{33})" "$1      ";
                              "~^(.{32})" "$1       ";
                              "~^(.{31})" "$1        ";
                              "~^(.{30})" "$1         ";
                              "~^(.{29})" "$1          ";
                              "~^(.{28})" "$1           ";
                              "~^(.{27})" "$1            ";
                              "~^(.{26})" "$1             ";
                              "~^(.{25})" "$1              ";
                              "~^(.{24})" "$1               ";
                              "~^(.{23})" "$1                ";
                              "~^(.{22})" "$1                 ";
                              "~^(.{21})" "$1                  ";
                              "~^(.{20})" "$1                   ";
                              "~^(.{19})" "$1                    ";
                              "~^(.{18})" "$1                     ";
                              "~^(.{17})" "$1                      ";
                              "~^(.{16})" "$1                       ";
                              "~^(.{15})" "$1                        ";
                              "~^(.{14})" "$1                         ";
                              "~^(.{13})" "$1                          ";
                              "~^(.{12})" "$1                           ";
                              "~^(.{11})" "$1                            ";
                              "~^(.{10})" "$1                             ";
                              "~^(.{9})"  "$1                              ";
                              "~^(.{8})"  "$1                               ";
                              "~^(.{7})"  "$1                                "; }

Isn't it beautiful?

I assume the performance impact of this is minimal? Yes it's hideous and a horrible hack, but it'll be minimal CPU to execute this on each request, I assume... but I've learned never to assume anything about nginx!

Sergey Serov avatar
de flag
Interesting! But so huge construction usually a flag that this way is wrong. I suppose it will be better to write bash-script (or any comfortable language) which will be parse log in the way You want.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.