Compare commits

...

2 Commits

Author SHA1 Message Date
Alexander Zhirov 7b6d13d9dc v0.2.0 2023-03-29 15:56:38 +03:00
Alexander Zhirov 3c096f9e5b v0.2.0 2023-03-29 15:53:00 +03:00
3 changed files with 86 additions and 60 deletions

16
CHANGELOG.md Normal file
View File

@ -0,0 +1,16 @@
# Changelog
## [v0.2.0](https://git.zhirov.kz/dlang/singlog/compare/v0.1.0...v0.2.0) (2023.03.29)
- Removed functions `fileOn()` and `fileOff()`
### New
- Simultaneous writing to the standard stream, syslog and file by binary setting the output flag
## [v0.1.0](https://git.zhirov.kz/dlang/singlog/commits/df602a8d0083249068b480e4a92cf7932f2c582b) (2023.03.23)
### The first stable working release
- Output to the standard stream or syslog
- Enable an entry in the file

View File

@ -1,5 +1,10 @@
# singlog
[![license](https://img.shields.io/github/license/AlexanderZhirov/singlog.svg?sort=semver&style=for-the-badge&color=green)](https://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
[![main](https://img.shields.io/badge/dynamic/json.svg?label=git.zhirov.kz&style=for-the-badge&url=https://git.zhirov.kz/api/v1/repos/dlang/singlog/tags&query=$[0].name&color=violet)](https://git.zhirov.kz/dlang/singlog)
[![githab](https://img.shields.io/github/v/tag/AlexanderZhirov/singlog.svg?sort=semver&style=for-the-badge&color=blue&label=github)](https://github.com/AlexanderZhirov/singlog)
[![dub](https://img.shields.io/dub/v/singlog.svg?sort=semver&style=for-the-badge&color=orange)](https://code.dlang.org/packages/singlog)
Singleton for simple logging
## Basic Usage
@ -9,10 +14,11 @@ import simplog;
void main()
{
Log.msg.level(Log.DEBUG);
Log.msg.output(Log.SYSLOG);
Log.msg.file("./file.log");
Log.msg.warning("Hello, World!");
log.level(log.DEBUG);
// write to syslog and file
log.output(log.SYSLOG | log.FILE);
log.file("./file.log");
log.warning("Hello, World!");
}
```
@ -21,42 +27,42 @@ void main()
Setting the error output level:
```d
Log.msg.level(Log.DEBUG);
Log.msg.level(Log.ALERT);
Log.msg.level(Log.CRIT);
Log.msg.level(Log.ERR);
Log.msg.level(Log.WARNING);
Log.msg.level(Log.NOTICE);
Log.msg.level(Log.INFO);
log.level(log.DEBUG);
log.level(log.ALERT);
log.level(log.CRIT);
log.level(log.ERR);
log.level(log.WARNING);
log.level(log.NOTICE);
log.level(log.INFO);
```
Assigning a target output:
```d
Log.msg.output(Log.SYSLOG);
Log.msg.output(Log.STDOUT);
log.output(log.SYSLOG);
log.output(log.STDOUT);
```
Setup and allowing writing to a file:
```d
Log.msg.file("./file.log");
Log.msg.fileOn();
Log.msg.fileOff();
log.output(log.FILE);
log.file("./file.log");
```
Output of messages to the log:
```d
Log.msg.alert("Alert message");
Log.msg.critical("Critical message");
Log.msg.error("Error message");
Log.msg.warning("Warning message");
Log.msg.notice("Notice message");
Log.msg.informations("Information message");
Log.msg.debugging("Debugging message");
log.alert("Alert message");
log.critical("Critical message");
log.error("Error message");
log.warning("Warning message");
log.notice("Notice message");
log.informations("Information message");
log.debugging("Debugging message");
```
## Dub
Add a dependency on `"singlog": "~>0.1.0"`.
Add a dependency on `"singlog": "~>0.2.0"`.

View File

@ -8,45 +8,51 @@ import std.file;
import std.datetime;
import datefmt;
alias log = log;
/++
Singleton for simple logging
---
// Setting the error output level
Log.msg.level(Log.DEBUG);
Log.msg.level(Log.ALERT);
Log.msg.level(Log.CRIT);
Log.msg.level(Log.ERR);
Log.msg.level(Log.WARNING);
Log.msg.level(Log.NOTICE);
Log.msg.level(Log.INFO);
log.level(log.DEBUG);
log.level(log.ALERT);
log.level(log.CRIT);
log.level(log.ERR);
log.level(log.WARNING);
log.level(log.NOTICE);
log.level(log.INFO);
// Assigning a target output
Log.msg.output(Log.SYSLOG);
Log.msg.output(Log.STDOUT);
log.output(log.SYSLOG);
log.output(log.STDOUT);
log.output(log.FILE);
// Setup and allowing writing to a file
Log.msg.file("./file.log");
Log.msg.fileOn();
Log.msg.fileOff();
log.file("./file.log");
log.fileOn();
log.fileOff();
// Output of messages to the log
Log.msg.alert("Alert message");
Log.msg.critical("Critical message");
Log.msg.error("Error message");
Log.msg.warning("Warning message");
Log.msg.notice("Notice message");
Log.msg.informations("Information message");
Log.msg.debugging("Debugging message");
log.alert("Alert message");
log.critical("Critical message");
log.error("Error message");
log.warning("Warning message");
log.notice("Notice message");
log.informations("Information message");
log.debugging("Debugging message");
---
+/
class Log
{
private static Log log;
private string path;
private bool writeToFile = false;
private bool fileExist = true;
private bool writeToFile = true;
private static SysTime time;
// Target output
enum {SYSLOG, STDOUT}
enum {
SYSLOG = 1,
STDOUT = 2,
FILE = 4
}
// Message output level
enum {
@ -68,33 +74,34 @@ class Log
{
if (this.msgLevel > msgLevel)
return;
if (this.msgOutput == STDOUT)
writeln(message);
else if (this.msgOutput == SYSLOG)
if (this.msgOutput & 1)
syslog(priority, (message ~ "\0").ptr);
writeFile(message);
if (this.msgOutput & 2)
writeln(message);
if (this.msgOutput & 4)
writeFile(message);
}
private void writeFile(string message)
{
if (!this.writeToFile || !this.fileExist)
if (!this.writeToFile)
return;
if (this.path.exists)
this.fileExist = true;
this.writeToFile = true;
else
{
this.fileExist = false;
this.writeToFile = false;
this.warning("The log file does not exist: " ~ this.path);
this.fileExist = true;
}
File file;
try {
file = File(this.path, "a+");
this.writeToFile = true;
} catch (Exception e) {
this.fileOff();
this.writeToFile = false;
this.error("Unable to open the log file " ~ this.path);
this.critical(e);
return;
@ -103,7 +110,7 @@ class Log
try {
file.writeln(this.time.format("%Y.%m.%d %H:%M:%S: ") ~ message);
} catch (Exception e) {
this.fileOff();
this.writeToFile = false;
this.error("Unable to write to the log file " ~ this.path);
this.critical(e);
return;
@ -112,7 +119,7 @@ class Log
try {
file.close();
} catch (Exception e) {
this.fileOff();
this.writeToFile = false;
this.error("Unable to close the log file " ~ this.path);
this.critical(e);
return;
@ -134,9 +141,6 @@ class Log
void level(int msgLevel) { this.msgLevel = msgLevel; }
void file(string path) { this.path = path; }
void fileOn() { this.writeToFile = true; }
void fileOff() { this.writeToFile = false; }
void alert(T)(T message) { writeLog(message.to!string, ALERT, LOG_ALERT); }
void critical(T)(T message) { writeLog(message.to!string, CRIT, LOG_CRIT); }
void error(T)(T message) { writeLog(message.to!string, ERR, LOG_ERR); }