Some thoughts on erlang log

Version information

  • Erlang18.3
  • Refer to the logger'formatter.erl file in Erlang 22.2

Templated log files

  • 1. Set template format
%% Default log template
    [[logger_formatter,header],"\n(",registername,":",pid,":",module,":",line, ")", mfa,":", msg, "\n"]).`

The default log template format is set. The final structure is:

  • 2. Code analysis
-type log_event() :: #{
					   level =>level(),   % Log printing level
					   msg => {io:format(),[term()]},  % Log content format
					   meta=>metadata()      % Additional required parameters  
-type metadata() ::{
					pid =>pid(),   % Process of log printing id
					gl => pid(),  % group_leader,
					time => interger(),  % time stamp
					mfa =>{module(),atom(),term()}, 
					line => non_neg_integer()  % Number of lines printed by log
						atom() => term()  % Other information
-type config() :: {
				   depath =>pos_integer() | unlimted,  % Indicates the length that can be displayed, use ... Express
				   legacy_header => boolean(),  %  Use default header or not
				   max_size =>non_neg_integer(),  % Maximum length
				   single_line => boolean(),  % Single line display or not
				   template => template()   % Custom template
-spec format(LogEvent, Config) -> string() when
			LogEvent :: log_event(),
			Config :: config().

The logevent parameter mainly refers to the settings related to message printing information. config is used to limit the format, length and other information of message printing

Data structure type of Erlang

  • tuple format
  • List list format
  • float
  • Integer integer
  • char character
  • String string
  • pid process
  • Refence() reference
  • atom() atom

The output format of Erlang

  • ~w
  • ~p/~tp
  • ~s/~ts

Erlang's data structure

  • record(record_tag{ arg1,arg2})
  • maps()

Character encoding

  • By default, erlang uses Latin1 (which can be viewed through io:printable_range())
  • Convert to unicode encoding: Unicode: characters? To? Binary()
  • Convert to utf8: xmerl? UCS: to? Utf8()

Clean up text formatting

string_p1([]) ->[];
string_p1([$\n|T]) -> string_p1(T);    % Newline character
string_p1([$\r|T]) -> string_p1(T);    % Carriage return character
string_p1([$\s|T]) -> string_p1(T);    % Blank space
string_p1([$\t|T]) -> string_p1(T);		%Horizontal tab
string_p1([$\v|T]) -> string_p1(T);		%vertical tab 
string_p1([$\b|T]) -> string_p1(T);		%Backspace
string_p1([$\f|T]) -> string_p1(T);		%Page key
string_p1([$\e|T]) -> string_p1(T);		%ESC(Code change-overflow)
string_p1([$#|T]) -> string_p1(T);
string_p1([H|T]) ->

Problems encountered in work

  • 1. After format is passed, the file cannot be written when writing; start erl setting + pc unicode
    Reason: because the file format after format is latin1, it cannot be written to the file; however, if the system stack is OK, it indicates that unicode has been encoded inside the system, but if it is in the code, the encoding format is latin1, which needs transcoding; after transcoding to unicode, there will be scrambling in the wer box, which needs to be noted.
  • 2. The log needs to be sent to the central server, but the data format of the central server is the conversion of asc code and cannot be displayed as Chinese characters
    Reason: unicode format cannot be saved directly to the database. The database setting format is utf-8, so you need to transcode unicode to utf-8
    Note: if there is Chinese, the utf8 transcoding directly for the generated string of format will result in an error. You need IO lib: format ("~ ts", [LogInfo]) before you can do it.
  • 3. In the remote http writing database, if the database reports an error, it will not be written in all the time. Later, it is found out that the central server log will detect the received information. If there are mysql related statements or special characters, they will fail
    Note: it is not allowed to send error reports directly to the database and filter special characters

test case

?ERR("player: ~w  not skill: ~w  add skill: ~w  old: ~w  new: ~w", [111111, [111,222], 584, [1,2,3],[4,5,6]]),
    ?ERR("Game player ,,,,,,,,"),
    ?ERR("Game player:~w  Skills that do not exist:~w  New skill points:~w  Old:~w  New:~w", [111111, [111,222], 584, [1,2,3],[4,5,6]]),
    ?DEBUG("Game player:~w  Skills that do not exist:~w  New skill points:~w  Old:~w  New:~w", [111111, [111,222], 584, [1,2,3],[4,5,6]]),
    ?WARNING("Game player:~w  Skills that do not exist:~w  New skill points:~w  Old:~w  New:~w", [111111, [111,222], 584, [1,2,3],[4,5,6]]),
    ?INFO("Game player:~w  Skills that do not exist:~w  New skill points:~w  Old:~w  New:~w", [111111, [111,222], 584, [1,2,3],[4,5,6]]),
    ?ERR("float: ~p,tuple = ~p, makeref=~p, list = ~tp, pid = ~p, int=~p ", [1.114, {1,2,3},make_ref(),[1,34,5],self(),123]),
    throw("test log"),

About Erl22.2 fish Erl18.3

  • 1.Erl22.2 has done many bottom-level optimizations and provided more abundant interface types
  • 2.Erl18.3 is a bit out of date. If you can, it is recommended to upgrade the version
  • 3. Although the old man also emphasizes the use of maps and wants to give up record, from the perspective of actual development, record also has its own advantages, and maps is really easy to use


  • 1. We must deal with problems in an orderly way so that we can solve them peacefully, quickly and safely
  • 2. Be sure to have a comprehensive understanding of the system, modify it thoroughly, and avoid missing it, resulting in some unpredictable errors
  • 3. test! Test! Test! Optimization! Optimization! Optimization!
  • 4. We must make clear the source code before we can make some changes. Otherwise, there will be many side effects. We also need to see the source code test cases to deepen our understanding
  • 5. When encountering a strange code, you must believe that you can understand it. It's only a matter of time. I saw this source code for about a week before I started to modify it and make my own customization

Tags: Programming Erlang Database encoding MySQL

Posted on Sat, 21 Mar 2020 05:10:06 -0400 by HairyArse