dlog/README.md

119 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

2023-03-02 09:55:38 +00:00
<p align="center">
<img src="branding/logo.png" width=220>
</p>
2021-12-23 12:55:37 +00:00
2023-03-02 09:55:38 +00:00
<br>
<h1 align="center">dlog</h1>
<h3 align="center"><i><b>Simple and modular logging library</i></b></h3>
---
<br>
<br
2021-12-23 10:14:36 +00:00
`[2021-Dec-23 11:17:35.3527637] (source/dlog/testing/thing.d:12): This is a log message`
---
2023-03-25 20:42:13 +00:00
[![D](https://github.com/deavmi/dlog/actions/workflows/d.yml/badge.svg)](https://github.com/deavmi/dlog/actions/workflows/d.yml)
2021-12-23 10:14:36 +00:00
## Usage
We recommend you use [dub](http://code.dlang.org) to add dlog to your project as follows:
```
dub add dlog
```
2021-12-23 13:06:55 +00:00
* [View on DUB](https://code.dlang.org/packages/dlog)
2024-04-10 16:05:07 +01:00
* [View API](https://dlog.dpldocs.info/)
2021-12-23 13:01:48 +00:00
2021-12-23 10:14:36 +00:00
### Components
dlog is formed out of two main components:
1. `Logger`
* The logger contains the needed barebones for facilitating the actual logging of text
2024-04-09 18:20:25 +01:00
* The _base logger_ (i.e. `Logger`) maintains a list of attaches _filters_, _message transformers_ and _handlers_
2. `Filter`
* Acts as a predicate on the incoming _message_ and determines whether it should be logged or not
* This is used by the `BasicLogger` to implement log levels
3. `Transform`
* A _message transform_ is attached to a logger and performs manipulation on the message logged
2021-12-23 10:14:36 +00:00
* They may be chained as to perform multiple transformations in a stream-like fashion
2024-04-09 18:20:25 +01:00
4. `Handler`
* A _handler_ handles the final transformed message, for some this means outputting to standard out, or a file
2021-12-23 10:14:36 +00:00
### Quick start
2024-04-10 16:05:07 +01:00
If you want to immediately begin logging text using the defaults and don't care about implementing your own transformations then you can
2023-03-03 13:11:11 +00:00
simply use the default logger as follows:
2021-12-23 10:14:36 +00:00
```d
import dlog;
2024-04-09 18:20:25 +01:00
DefaultLogger logger = new DefaultLogger();
2021-12-23 10:14:36 +00:00
2024-04-09 18:20:25 +01:00
logger.setLevel(Level.DEBUG);
2023-03-03 13:11:11 +00:00
logger.error(["woah", "LEVELS!"], 69.420);
logger.info(["woah", "LEVELS!"], 69.420);
logger.warn(["woah", "LEVELS!"], 69.420);
logger.debug_(["woah", "LEVELS!"], 69.420);
2024-04-09 18:20:25 +01:00
// Should not be able to see this
logger.setLevel(Level.INFO);
logger.debug_("Can't see me!");
2023-03-03 13:11:11 +00:00
```
2024-04-09 18:20:25 +01:00
This will output the following:
2023-03-03 13:11:11 +00:00
```
2024-04-09 18:20:25 +01:00
[2024-Apr-09 19:14:38.3077171] (ERROR): ["woah", "LEVELS!"] 69.42
[2024-Apr-09 19:14:38.3077346] (INFO): ["woah", "LEVELS!"] 69.42
[2024-Apr-09 19:14:38.3077559] (WARN): ["woah", "LEVELS!"] 69.42
[2024-Apr-09 19:14:38.3077759] (DEBUG): ["woah", "LEVELS!"] 69.42
2023-03-03 13:11:11 +00:00
```
2024-04-10 16:05:07 +01:00
You can see the [full API](https://dlog.dpldocs.info/) for more information.
2023-03-03 13:11:11 +00:00
2021-12-23 10:14:36 +00:00
### Custom loggers
2021-12-25 08:44:52 +00:00
#### Implementing your own transform
2021-12-23 10:14:36 +00:00
Perhaps the default transformation, `DefaultTransform`, may not be what you want. Maybe you want the module name included in the logged
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
message transform.
2024-04-09 18:20:25 +01:00
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:
2021-12-23 10:14:36 +00:00
```d
import dlog;
2024-04-10 16:05:07 +01:00
public class CustomTransform : Transform
2021-12-23 10:14:36 +00:00
{
2024-04-09 18:20:25 +01:00
public override Message transform(Message message)
2021-12-23 10:14:36 +00:00
{
2024-04-09 18:20:25 +01:00
BasicMessage bmesg = cast(BasicMessage)message;
// Only handle BasicMessage(s) - ones which have `setText(string)`
if(bmesg is null)
{
return message;
}
2021-12-23 10:14:36 +00:00
2024-04-09 18:20:25 +01:00
string transformed;
/* Insert transformation code here */
bmesg.setText(transformed);
2021-12-23 10:14:36 +00:00
2024-04-09 18:20:25 +01:00
return message;
2021-12-23 10:14:36 +00:00
}
}
```
## License
2023-03-25 20:42:13 +00:00
LGPL v3