Skip to content

Commit 55de4b7

Browse files
authored
Merge pull request #5 from deavmi/feature/nextgen
DLog v2 - Complete re-write
2 parents 0309e28 + dac2318 commit 55de4b7

File tree

8 files changed

+751
-769
lines changed

8 files changed

+751
-769
lines changed

README.md

Lines changed: 34 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,15 @@ dlog is formed out of two main components:
3737

3838
1. `Logger`
3939
* The logger contains the needed barebones for facilitating the actual logging of text
40-
2. `MessageTransform`
41-
* A MessageTransform is attached to a logger and performs manipulation on the text input into the logger for logging
40+
* The _base logger_ (i.e. `Logger`) maintains a list of attaches _filters_, _message transformers_ and _handlers_
41+
2. `Filter`
42+
* Acts as a predicate on the incoming _message_ and determines whether it should be logged or not
43+
* This is used by the `BasicLogger` to implement log levels
44+
3. `Transform`
45+
* A _message transform_ is attached to a logger and performs manipulation on the message logged
4246
* They may be chained as to perform multiple transformations in a stream-like fashion
47+
4. `Handler`
48+
* A _handler_ handles the final transformed message, for some this means outputting to standard out, or a file
4349

4450
### Quick start
4551

@@ -49,60 +55,29 @@ simply use the default logger as follows:
4955
```d
5056
import dlog;
5157
52-
Logger logger = new DefaultLogger();
58+
DefaultLogger logger = new DefaultLogger();
5359
54-
55-
logger.log("This is a log message");
56-
logger.log(1);
57-
logger.log(1==1);
58-
logger.log([1,2,3]);
59-
```
60-
61-
This will output the following:
62-
63-
```
64-
[2021-Dec-23 11:17:35.3527637] (source/dlog/testing/thing.d:12): This is a log message
65-
[2021-Dec-23 11:17:35.3527717] (source/dlog/testing/thing.d:13): 1
66-
[2021-Dec-23 11:17:35.3527789] (source/dlog/testing/thing.d:14): true
67-
[2021-Dec-23 11:17:35.3527871] (source/dlog/testing/thing.d:15): [1, 2, 3]
68-
```
69-
70-
As you can see file and line numbering of where the `log()` function is called appears in the log message which can be quite helpful
71-
for debugging.
72-
73-
---
74-
75-
We also support many different logging levels which can be accomplished using the `error`, `debug_` (or the `dbg` alias), `info `(the default) and `warn`:
76-
77-
```d
78-
Logger logger = new DefaultLogger();
79-
80-
// Create a default logger with the default joiner
81-
logger = new DefaultLogger();
82-
83-
// Test out `error()`
60+
logger.setLevel(Level.DEBUG);
8461
logger.error(["woah", "LEVELS!"], 69.420);
85-
86-
// Test out `info()`
8762
logger.info(["woah", "LEVELS!"], 69.420);
88-
89-
// Test out `warn()`
9063
logger.warn(["woah", "LEVELS!"], 69.420);
91-
92-
// Test out `debug_()`
9364
logger.debug_(["woah", "LEVELS!"], 69.420);
65+
66+
// Should not be able to see this
67+
logger.setLevel(Level.INFO);
68+
logger.debug_("Can't see me!");
9469
```
9570

96-
This outputs the following:
71+
This will output the following:
9772

9873
```
99-
[2023-Mar-03 11:33:49.2617904] (source/dlog/core.d:427): ["woah", "LEVELS!"] 69.42
100-
[2023-Mar-03 11:33:49.2618091] (source/dlog/core.d:430): ["woah", "LEVELS!"] 69.42
101-
[2023-Mar-03 11:33:49.2618273] (source/dlog/core.d:433): ["woah", "LEVELS!"] 69.42
102-
[2023-Mar-03 11:33:49.2618457] (source/dlog/core.d:436): ["woah", "LEVELS!"] 69.42
74+
[2024-Apr-09 19:14:38.3077171] (ERROR): ["woah", "LEVELS!"] 69.42
75+
[2024-Apr-09 19:14:38.3077346] (INFO): ["woah", "LEVELS!"] 69.42
76+
[2024-Apr-09 19:14:38.3077559] (WARN): ["woah", "LEVELS!"] 69.42
77+
[2024-Apr-09 19:14:38.3077759] (DEBUG): ["woah", "LEVELS!"] 69.42
10378
```
10479

105-
You can also look into `logc(Context, string)` which allows you to use a `Context` object when logging, more information available in the [full API](https://dlog.dpldocs.info/v0.3.19/dlog.context.html).
80+
You can see the [full API](https://dlog.dpldocs.info/v0.3.19/dlog.context.html) for more information.
10681

10782
### Custom loggers
10883

@@ -112,64 +87,32 @@ Perhaps the default transformation, `DefaultTransform`, may not be what you want
11287
messages or perhaps don't want the date-and-timestamp included at all. All of this can be up to you if you choose to implement your own
11388
message transform.
11489

115-
You will need to start off with a class that inherits from the `MessageTransform` class and then which overrides the `transform` method as shown below:
90+
You will need to start off with a class that inherits from the `Transform` class and then which overrides the `transform` method as shown below:
11691

11792
```d
11893
import dlog;
11994
120-
public class CustomTranform : MessageTransform
95+
public class CustomTranform : Transform
12196
{
122-
public override string transform(string text, Context context)
97+
public override Message transform(Message message)
12398
{
124-
string transformed;
99+
BasicMessage bmesg = cast(BasicMessage)message;
100+
101+
// Only handle BasicMessage(s) - ones which have `setText(string)`
102+
if(bmesg is null)
103+
{
104+
return message;
105+
}
125106
126-
/* Insert code to transform `text` and return the `transformed` text */
107+
string transformed;
108+
/* Insert transformation code here */
109+
bmesg.setText(transformed);
127110
128-
return transformed;
111+
return message;
129112
}
130113
}
131114
```
132115

133-
Additional information, besides the text being logged itself (this is the `string text` argument), comes in the form of a `Context` object `context`. What one can get from this is a `CompilationInfo` struct which contains the following fields below if one calls `toArray()` on
134-
it which will return a string array shown below (we refer to this array as `lineInfo`):
135-
136-
1. `lineInfo[0]`
137-
* This contains `__FILE_FULL_PATH__` which is the full path (absolute) to the source file where `log()` was called
138-
2. `lineInfo[1]`
139-
* This contains `__FILE__` which is the path (starting at `source/` to the source file where `log()` was called
140-
3. `lineInfo[2]`
141-
* This contains a stringified version of `__LINE__` which is the line number of the call to `log()`
142-
4. `lineInfo[3]`
143-
* This contains `__MODULE__` which is the name of the module the call to `log()` appeared in
144-
5. `lineInfo[4]`
145-
* This contains `__FUNCTION__` which is the name of the function `log()` was called in
146-
6. `lineInfo[5]`
147-
* This contains `__PRETTY_FUNCTION__` which is the same as above but with type information
148-
149-
The point of a `Context` object is also such that a custom transformer may expect a kind-of `Context` like a custom one (i.e. `CustomContext`)
150-
which perhaps a custom logger (kind-of `Logger`) can then have set certain fields in it.
151-
152-
## Creating a Logger
153-
154-
We now need to create a logger that makes use of our message transform, we can do so by creating an instance
155-
of the `Logger` class and passing in our `MessageTransform` as so:
156-
157-
```d
158-
Logger customLogger = new DefaultLogger(new CustomTranform());
159-
```
160-
161-
The above is all one needs to be able to pull off a custom transformation.
162-
163-
### Custom Logger
164-
165-
Custom loggers can also be created by sub-classing the `Logger` class and overriding the `logImpl(string)` method.
166-
The reason someone may want to do this is up to them. One easy to think of reason is to perhaps applying filtering
167-
of messages to be logged and skip them (as this method is where the I/O of printing out the logs normally happens).
168-
Another reason may be to log to a different data resource, the `DefaultLogger` writes to the file descriptor `0` (stdout),
169-
but you may want to log over a socket connection to a remote machine for example, or perhaps do several pieces of
170-
I/O for your logging. One can do that with a custom logger, you shoudl see `source/dlog/defaults.d` for the implementation
171-
of a custom logger, such as `DefaultLogger`.
172-
173116
## License
174117

175118
LGPL v3

0 commit comments

Comments
 (0)