Repository navigation
Expand file tree
/
Copy pathmatlogExample.m
More file actions
147 lines (125 loc) · 5.75 KB
/
Copy pathmatlogExample.m
File metadata and controls
147 lines (125 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
%% Logging Example
% Learn how to create and use basic loggers.
% Your path will need to include the parent folder of +matlog.
%% Section 1a Simplest logger
% Create a logger, check the level, and then clear up.
% Configure the logger in the simplest way and then create our logger.
% The logging class contains the user interaction functions.
% Begin by importing the `matlog.logging` class. Now we can access it using
% just `logging`.
import matlog.logging
logging.basicConfig();
logger = matlog.logging.getLogger();
fprintf("Log level is %s\n", logger.level);
logger.error("This should be logged");
logger.info("This should not be logged");
% We finish by clearing up the logger using the logging access-point.
logging.clear();
% After using `clear` the loggers won't work any more.
logger.error("The logger has stopped logging.")
%% Section 1b Alternative to importing
% We can access the logging capability without importing by prefixing
% references with matlog.
matlog.logging.basicConfig();
logger = matlog.logging.getLogger();
fprintf("Log level is %s\n", logger.level);
logger.error("This should be logged");
matlog.logging.clear();
%% Section 2a Getting to know basicConfig
% We can set the level, enable logging to file, and set the log format.
% After running, check that a local file `example.log` has been created.
% Notice how the log messages have gone to two different places: the console
% and a file.
import matlog.logging
logging.basicConfig('level', 'INFO', 'logfile', 'example.log');
logger = logging.getLogger();
fprintf("Log level is %s\n", logger.level);
logger.error("This should be logged");
logger.info("This will now be logged too");
logging.clear();
%% Section 2b Getting to know basicConfig - formatting
% We can use basicConfig to control the formatting of the logs.
% There are many fields you can include in the format string. The format string
% describes the full text that appears in each log - fields, delimiters, and
% any extra content. The fields must be specified using the form `%(name)s`,
% where name is the case-sensitive name given to the field in LogRecord, and
% `s` in this case specifies the string-type formatting. (For numeric fields
% like line number, change this to `d`).
% Do not include anything in the form %(name) that isn't a valid field.
import matlog.logging
formatStr = '%(asctime)s ~ %(filename)s[%(lineno)d]';
logging.basicConfig('level', 'INFO', 'format', formatStr);
logger = logging.getLogger();
logger.warning("This message is not included");
logging.clear();
%% Section 3 LogHandler
% In Section 2 we saw that the log messages were sent to two places.
% This is possible because basicConfig configure the logger to have two
% `LogHandler`s:a StreamHandler for the console output, and a FileHandler for
% logging to file.
% We can set the loggers manually, too.
import matlog.logging matlog.StreamHandler
logger = logging.getLogger();
% Initially there aren't any handlers and so the log messages will not go
% anywhere.
display(logger.handlers);
logger.error("This message won't be seen because there are no handlers");
handler1 = StreamHandler();
logger.addHandler(handler1);
logger.error("Now we have a logger we can see the message");
% We can add another logger with a different format. You can even change how
% the time is represented.
% The handlers have their own log level filter which operate in addition to the
% logger's level. By default they allow all levels.
% By default StreamHandlers log to stdout.
handler2 = StreamHandler('format', '%(asctime)s %(level)s: %(message)s',...
'dateFormat', 'yyyy MMM dd - HHmmss', 'level', 'ERROR');
logger.addHandler(handler2);
logger.error("You should see this twice");
logger.warning("This priority will only be logged by one handler");
logging.clear();
%% Section 4 Logging hierarchy
% We can have several loggers at the same time. A logger may have any number of
% handlers, including 0.
% Any logs of sufficient level will be passed to their handlers, and then onto
% their parent.
% The hierarchy is determined by the name property, passed to getLogger().
% If you don't specify a name, the root logger is returned. This is in the
% ancestry tree of all loggers.
% Generations of loggers are dot-separated. A name of "some.logger" is the
% parent to "some.logger.child".
%
% We create a hierarchy of loggers with different levels.
import matlog.logging matlog.StreamHandler matlog.LogLevel
% basicConfig configures the root logger only.
rootLogger = logging.getLogger();
rootLogger.level = LogLevel.WARNING;
fatherLogger = logging.getLogger('father');
fatherLogger.level = LogLevel.INFO;
daughterLogger = logging.getLogger('father.daughter');
daughterLogger.level = LogLevel.ALL;
% Check the names given to the loggers
display([rootLogger.name fatherLogger.name daughterLogger.name]);
display([rootLogger.parent fatherLogger.parent.name daughterLogger.parent.name]);
% root logs to stderr and the others to stdout.
rootLogger.addHandler(StreamHandler(2));
fatherLogger.addHandler(StreamHandler());
daughterLogger.addHandler(StreamHandler());
% Log different levels to demonstrate the filtering effect of the hierarchy.
% Notice the logger name in each log message.
daughterLogger.warning("This message is logged by all loggers");
daughterLogger.info("This message is logged by only father and daughter");
daughterLogger.debug("This message is logged by only daughter");
logging.clear();
%% Section 5 Special characters
% To log certain escape characters such as `'%'` and `'\'` you should enter them
% as `'%%'` and `'\\'` respectively.
import matlog.logging
logging.basicConfig('level', 'INFO');
logger = logging.getLogger();
logger.info('50%% complete');
% You can alternatively make use of string formatting:
logger.info('%s', '100% complete');
% Now for paths:
logger.info('C:\\User\\me');
logger.info('%s', 'C:\User\you');