ALEMIL

Welcome to a blog about application and game development

How logging can simplify complex problems

How logging can simplify complex problems 2018-07-01 20:43:56
Est. read time: 5 minutes, 32 seconds

Working with logs is a very significant part of software development. Logging means writing messages to a file (or console) describing what is happening in the application. It allows monitoring of complex systems and step by step description of its behavior.

What problems does logging solve


When you develop simple systems, you might not need to implement logging. All you need is a debugger for your language of choice and move from breakpoint to breakpoint analyzing what is going on, how values of your variables are changing.

Sometimes you will implement a bit more complex features and logging can help to simplify them. Lately, I had some issues when working with pathfinding for a 2d tile based game. From time to time the algorithm would freeze the whole game, eating too much CPU. Can you imagine pinpointing this situation with a debugger if you had about 50 AI agents that might want to go anywhere in a 192x192 grid? Well, that would be a nightmare.

Instead of debugging you could insert logging in specific points of your codebase and write proper messages describing what is going on. In the example above we might want to start by writing something like this to a text file:

[2018-07-01 12:21:05.608] [ai_path] [info] Entity 840 is moving: from 36,41 to 30,41
[2018-07-01 12:21:05.608] [ai_path] [info] Shortest path calculated in 34 ms: 6 tiles, last tile 30,41

By logging these 2 lines we can now understand where our entities want to go and how long is the path they want to take. We also identify when the last tile of the calculated path is different than what we expect.

Now when we happen to have a freeze again, we can check our log file and find which entity is breaking our game. It turned out that an entity could not get to a specific tile on the map, because there was no way available. About 36863 tiles were scanned for a potential path, but with no luck. The solution was to change the action planner to not allow picking unavailable targets and throw an exception when this situation happens again.

There is another interesting use case for logs that I stumbled upon a few months back. I had to implement an algorithm determining advertisement emission. Which ad, at which place should be displayed, given many different conditions. Even with multiple unit tests covering the codebase ensuring specific calculations return proper results, there is a risk that a whole system combining these components will not work properly.

In such case, especially when writing TDD, it's possible to test if logs are created exactly as we expect, given a specific input. It allows us to implement big features partially while testing along the way. To do this, we need to parse logs created in the test, finding if message exists.

// handler is pointing to a file holding log messages
this->checkLogExist(handler, 'Entity 4 action picked: Fishing');

The same solution can be applied when maintaining a monolithic project that doesn't work as intended and is difficult to unit test because of too many dependencies. Adding logging and some automated feature tests to old codebase can simplify it and serve as a data flow explanation.

How to write useful log messages


When writing log messages you should try to provide answers to some or all of the below questions:

  • When was the message collected? Write time and/or date depending on your needs. In production, the date might inform you if the bug happened before or after the recent patch.

  • Where was the message broadcasted? Write thread name, hostname, web application route, or class name the logger is working on.

  • What is the log level of the message? Is it a debug, info or error message? Is it critical - did your application break?

  • Why was the message produced? What conditions had to be matched for this message to be logged, what values are being held by variables that affect the state of your code?


Apart from the above, if you distribute your application to multiple machines, you might want to write the system and hardware information at the beginning of the file.

Below you can find some example logs for world creation I used in a roguelike game:
[07/01/18 20:22:03] [thread 10152] [info] Registered world locations 47, designs loaded 47
[07/01/18 20:22:03] [thread 10152] [info] Loading organizations for world maps:
[07/01/18 20:22:03] [thread 10152] [info] [1] Kabula - map 30,30,0
[07/01/18 20:22:03] [thread 10152] [info] [2] Kar Laduhr Dwarves - map 33,30,0
[07/01/18 20:22:03] [thread 10152] [info] [3] Forest folk I - map 29,29,0
[07/01/18 20:22:03] [thread 10152] [info] [4] Animals I - map 30,29,0
[07/01/18 20:22:03] [thread 10152] [info] [5] Anheluna Elves - map 29,28,0
[07/01/18 20:22:03] [thread 10152] [info] [6] Mountain Pass - map 32,30,0
[07/01/18 20:22:03] [thread 10152] [info] Starting world creation thread
[07/01/18 20:22:03] [thread 9980] [info] Starting design 30, 30, 0
[07/01/18 20:22:03] [thread 9980] [info] Adding map Kabula to world
[07/01/18 20:22:04] [thread 9980] [info] Player entity spawned at 30, 28
[07/01/18 20:22:04] [thread 9980] [info] Created 839 entities on local map
[07/01/18 20:22:04] [thread 9980] [info] Spawning organization id 1
[07/01/18 20:22:04] [thread 9980] [info] Entity [840] Human woodcutter spawned at position 35, 58
[07/01/18 20:22:04] [thread 9980] [info] Entity [843] Human woodcutter spawned at position 4, 37
[07/01/18 20:22:04] [thread 9980] [info] Entity [846] Human woodcutter spawned at position 56, 57
[07/01/18 20:22:04] [thread 9980] [info] Entity [849] Human woodcutter spawned at position 33, 40
[07/01/18 20:22:04] [thread 9980] [info] Entity [852] Human woodcutter spawned at position 36, 24
[07/01/18 20:22:04] [thread 9980] [info] Entity [855] Human fisherman spawned at position 55, 49
[07/01/18 20:22:04] [thread 9980] [info] Entity [858] Human fisherman spawned at position 21, 3
[07/01/18 20:22:04] [thread 9980] [info] Entity [862] Human fisherman spawned at position 24, 52
[07/01/18 20:22:04] [thread 9980] [info] Entity [866] Human blacksmith spawned at position 27, 11
[07/01/18 20:22:04] [thread 9980] [info] Entity [868] Magnus the Magnificent spawned at position 49, 19
[07/01/18 20:22:04] [thread 9980] [info] Local map created and saved
[07/01/18 20:22:04] [thread 9980] [info] Starting design 31, 30, 0
[07/01/18 20:22:04] [thread 9980] [info] Adding map Mountain Pass to world
[07/01/18 20:22:04] [thread 9980] [info] Created 594 entities on local map
[07/01/18 20:22:04] [thread 9980] [info] Local map created and saved
[07/01/18 20:22:04] [thread 9980] [info] Starting design 32, 30, 0
[07/01/18 20:22:04] [thread 9980] [info] Adding map Yelling cavern to world
[07/01/18 20:22:04] [thread 9980] [info] Created 3300 entities on local map
[07/01/18 20:22:04] [thread 9980] [info] Spawning organization id 6
[07/01/18 20:22:04] [thread 9980] [info] Entity [4941] Troglodyte spawned at position 45, 30
[07/01/18 20:22:04] [thread 9980] [info] Entity [4943] Troglodyte spawned at position 14, 32
[07/01/18 20:22:04] [thread 9980] [info] Entity [4945] Troglodyte spawned at position 27, 38
[07/01/18 20:22:04] [thread 9980] [info] Entity [4947] Troglodyte spawned at position 26, 50
[07/01/18 20:22:04] [thread 9980] [info] Local map created and saved
[07/01/18 20:22:04] [thread 9980] [info] Starting design 32, 30, -1
[07/01/18 20:22:04] [thread 9980] [warning] Skipping map design 32, 30, -1: no name
[07/01/18 20:22:07] [thread 9980] [info] Finished world creation, loaded player map
[07/01/18 20:22:07] [thread 10152] [info] Closing world creation thread

Conclusion


Logging messages can help you with debugging complex systems, describing how they work, applying monitoring, or even help with automated testing. When implemented correctly, you can follow the flow of your application step by step, like a diary.

On the last note, you don't have to implement logging on your own. Most languages will have libraries that can speed up the process. For C++ I can recommend spdlog, for PHP Monolog and for Java - logback. These libraries have already multiple potential issues solved for you, like thread-safety, logging to multiple files, splitting files by dates, writing different variable types and many more. If you are ever in doubt of what is happening in your application - log it.



Comments +

MAkCorps API
Breakpoints is a best alternative of logging.
2018-07-01 21:21:07
Reply
rardzer
Hello. And Bye.
2019-07-01 11:05:49
Reply
RickyCon
As a result a situation that you have avoid at all cost. Actually, dark rings the particular eyes do not develop as a melanin film. Or you can find a basement ventilation system.I extremely hesitation you always be mailing it back. So exactly what is the best solution in these difficult instances? It will eventually clear up, however in the meantime, it's not pretty or comfortable.

Verlaven аптека
NPA TOUCH beoordelingen

We would whine at nap time and delay bed as long as you can. By purchasing one of several available fishing boat kits he can build his own boat. These acids are the primary components of complex healthy proteins.Reaction time is also significantly decreased through lack of sleep. Causes chickens to spend much of time lying down, since walking is too painful. But wait, you don't require to spend your precious money just yet!

крем за избелване на лице
ecoslim usa

Around this temperature, your potatoes will keep for at a minimum a season. The potato is one of essentially the most popular and versatile foods doable ! cook with in the American kitchen.You can also hire a profitable business that cleans ducts to inspect the ducts you have right proper. Whenever we keep on doing this, then perhaps we is a clear idea of what ghosts are set up of.


atiora вилучення та виплата
mfc investment voor mobiele app

Don't call, text, e mail your ex a thousand times each day. So even this is raining or drizzling, the Ferrari umbrella would definitely be the perfect companion. Holiday home rentals allows cooking your own food.And ventilation could also mean presently there is no foul smell inside initially. Give this article a read for security alarm advice. This can be a large amount of as two months prior to your occasion.

iq option seçenek stratejileri
forex wings stratégies d'options


Whatever you choose, your getaway room will be perfect using a little assist of your local contractor. Make sure to fit your wire dog crate to pet dog before fitting it for ones car.This will not be a chicken feed, as a few more experienced poultry farmers would say. While leaping your home to run well, you will need to pay focus to visual details, too.
2019-07-31 15:41:50
Reply
EgorBroca
anal xtube nsfw gif
2019-08-02 17:16:34
Reply
jejanElini
buy viagra online
2019-08-14 19:17:24
Reply
SVSunlara
Некоторые из скважин в конце выполнения восстановительного комплекса действий имеют возможность быть снова введены в использование. Тем более что цена этих выполненных работ в десятки раз ниже общей стоимости самих сооружений.
Гарантировано повышение сбыта водозаборной скважины не менее 30 процентов от имеющегося на момент старта выполненных работ.

В восьмидесятиS% случаях скважины возобновляются до основных данных при введении в применение скважины, что является альтернативой бурения новой скважины.

Сотрудники нашей предприятие по Очистке колодцев, отстойников и Очистке водоподъемных труб предлагаем свои услуги всем, как частным так и общественным системам.

SpecVodService : очистка скважины от глины
2019-08-19 06:46:12
Reply
RubenNiB
Приветствую! интересный у вас сайт!
Нашел класную базу кино: мелодрамы 2018 года хорошего качества http://kinoserialtv.net/
Тут: лучшие российские боевики новинки бесплатно русские боевики 2018 бесплатно в хорошем список 2018
Тут: фильмы фантастика новинки онлайн хорошем фантастика смотреть онлайн хорошем качестве hd 720 рейтинг 2018
Тут: http://kinoserialtv.net/15950-korol-lev-zarabotal-milliard-dollarov.html «Король Лев» заработал миллиард долларов
Тут: Пошли в тюрьму / Let's Go to Prison (2006) смотреть онлайн бесплатно Пошли в тюрьму / Let's Go to Prison (2006) смотреть онлайн бесплатно
2019-08-20 20:36:31
Reply
Allendiemo
Gumivore-Fare Diet for Sugar Gliders and Marmosets 8 oz. Jar

Spark. Create. Imagine. Walk Along Elephant


http://pegasbaby.com/com/shop/view/212698/
2019-08-21 21:57:55
Reply
Dames kah
As a rule, having asked a question of installation of a ladder in the house, owners don't assume that the ladder and its protection are two absolutely different products. Well, if the same company, like us, is engaged in the arrangement of turnkey facilities and can offer to make a ladder and metal fences on it in one set, but in practice it is rare. Therefore, before any owner of the stairs in the house the question arises: how to make it safe, we guarantee the best conditions for cooperation thanks to our own production, high potential design office and focus on long-term mutually beneficial partnership. Here you can buy not only standard fencing for stairs, but also design, made to order and is amazingly cheap. New construction technologies, developing rapidly, allow to use light, modern, reliable and weightless structures. Stair railings made of stainless steel are such that the price thereof is acceptable. Railings made of reliable material: an important structural element, part of the design of the building, the guarantors of safety and easy descent and ascent the stairs. Protections for ladders which are offered by our company differ in reliability, resistance to various aggressive influences and faultless appearance. In addition, their manufacture takes into account all kinds of standards and requirements relevant to this group of products. It is difficult to imagine a building in which there will be no stair railings, the presence of which increases the convenience, safety when moving. Note that today for the manufacture of construction offers a large selection of elements with which it is possible to quickly perform the installation of the structure, which for many years will last without losing the original qualities. Manufacturing and installation of stainless steel stairs is inexpensive compared to designs from other materials with equal quality characteristics. However, stainless steel is much more popular material-it is easy to handle, combine with other materials, install and care for the finished product
2019-09-04 18:40:54
Reply
Jameschons
Рыболовная база
2019-09-05 18:16:11
Reply
fxbrokersTold
currency-trading-brokers.com/forex-comparisons-ratings-reviews-ireland.html Seiceáil is fearr trádála-trádáil bróicéirí.
2019-09-10 08:20:28
Reply
Jerrysjuild
svenska casino bonus utan insättning

bedste online casino

casino
2019-09-19 02:42:51
Reply
Normankeymn
Professional cleaning today is much more than just cleansing: contemporary devices, devices and also techniques of work transform it into a genuine modern process that substantially transforms the standard understanding of what it means to truly "clean".

In residence cleaning company - excellent
Best maid NY
- it is actually quick and easy, practical and also budget-friendly along with our company.
Trust fund the hygiene leaders to house cleaning Brooklyn! Our team make use of expert detergents and also technological devices of international suppliers in our work and carry out a great work with cleaning of any sort of complexity.

8]Cleaning services franchise ny

Collaboration along with the business is the backer of an impressive, lucrative as well as effective cleaning of specialist residence cleaning and bordering places. Currently, house cleaning company from our firm are utilized in New Jacket. Value and also you the true advantages as well as functional advantages of our organisation proposal.

By signing an irreversible company agreement along with our team, you will have the capacity to desert the need to sustain a sizable personnel of technical personnel, which, subsequently, are going to maximize costs. The price of the facility, daily, overall home cleansing of Staten Island, performed by our staff members, will definitely always be actually less than the price of wages for cleaning services, the investment of cleansing products and devices.

6]Affordable house cleaning services manhattan

Leave behind a request on the website, define your label or provider label, get in touch with telephone number and also time of desired cleaning, leave your wants as well as demands in an information to the supervisor if essential, after that our expert will certainly call you in the least opportunity and also specify the time, time as well as work place!
2019-09-19 03:25:56
Reply
OlegSal
Регистрация
2019-09-23 00:21:37
Reply