1
0
Fork 0
This commit is contained in:
Alexander Zhirov 2023-03-29 15:53:00 +03:00
parent df602a8d00
commit 3c096f9e5b
3 changed files with 85 additions and 60 deletions

15
CHANGELOG.md Normal file
View File

@ -0,0 +1,15 @@
# Changelog
## [v0.2.0](https://git.zhirov.kz/dlang/singlog/compare/v0.1.0...v0.2.0) (2023.03.29)
### Bug fixes
- Removed functions `fileOn()` and `fileOff()`
- 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 # 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 Singleton for simple logging
## Basic Usage ## Basic Usage
@ -9,10 +14,11 @@ import simplog;
void main() void main()
{ {
Log.msg.level(Log.DEBUG); log.level(log.DEBUG);
Log.msg.output(Log.SYSLOG); // write to syslog and file
Log.msg.file("./file.log"); log.output(log.SYSLOG | log.FILE);
Log.msg.warning("Hello, World!"); log.file("./file.log");
log.warning("Hello, World!");
} }
``` ```
@ -21,42 +27,42 @@ void main()
Setting the error output level: Setting the error output level:
```d ```d
Log.msg.level(Log.DEBUG); log.level(log.DEBUG);
Log.msg.level(Log.ALERT); log.level(log.ALERT);
Log.msg.level(Log.CRIT); log.level(log.CRIT);
Log.msg.level(Log.ERR); log.level(log.ERR);
Log.msg.level(Log.WARNING); log.level(log.WARNING);
Log.msg.level(Log.NOTICE); log.level(log.NOTICE);
Log.msg.level(Log.INFO); log.level(log.INFO);
``` ```
Assigning a target output: Assigning a target output:
```d ```d
Log.msg.output(Log.SYSLOG); log.output(log.SYSLOG);
Log.msg.output(Log.STDOUT); log.output(log.STDOUT);
``` ```
Setup and allowing writing to a file: Setup and allowing writing to a file:
```d ```d
Log.msg.file("./file.log"); log.output(log.FILE);
Log.msg.fileOn(); log.file("./file.log");
Log.msg.fileOff();
``` ```
Output of messages to the log: Output of messages to the log:
```d ```d
Log.msg.alert("Alert message"); log.alert("Alert message");
Log.msg.critical("Critical message"); log.critical("Critical message");
Log.msg.error("Error message"); log.error("Error message");
Log.msg.warning("Warning message"); log.warning("Warning message");
Log.msg.notice("Notice message"); log.notice("Notice message");
Log.msg.informations("Information message"); log.informations("Information message");
Log.msg.debugging("Debugging message"); log.debugging("Debugging message");
``` ```
## Dub ## 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 std.datetime;
import datefmt; import datefmt;
alias log = log;
/++ /++
Singleton for simple logging Singleton for simple logging
--- ---
// Setting the error output level // Setting the error output level
Log.msg.level(Log.DEBUG); log.level(log.DEBUG);
Log.msg.level(Log.ALERT); log.level(log.ALERT);
Log.msg.level(Log.CRIT); log.level(log.CRIT);
Log.msg.level(Log.ERR); log.level(log.ERR);
Log.msg.level(Log.WARNING); log.level(log.WARNING);
Log.msg.level(Log.NOTICE); log.level(log.NOTICE);
Log.msg.level(Log.INFO); log.level(log.INFO);
// Assigning a target output // Assigning a target output
Log.msg.output(Log.SYSLOG); log.output(log.SYSLOG);
Log.msg.output(Log.STDOUT); log.output(log.STDOUT);
log.output(log.FILE);
// Setup and allowing writing to a file // Setup and allowing writing to a file
Log.msg.file("./file.log"); log.file("./file.log");
Log.msg.fileOn(); log.fileOn();
Log.msg.fileOff(); log.fileOff();
// Output of messages to the log // Output of messages to the log
Log.msg.alert("Alert message"); log.alert("Alert message");
Log.msg.critical("Critical message"); log.critical("Critical message");
Log.msg.error("Error message"); log.error("Error message");
Log.msg.warning("Warning message"); log.warning("Warning message");
Log.msg.notice("Notice message"); log.notice("Notice message");
Log.msg.informations("Information message"); log.informations("Information message");
Log.msg.debugging("Debugging message"); log.debugging("Debugging message");
--- ---
+/ +/
class Log class Log
{ {
private static Log log; private static Log log;
private string path; private string path;
private bool writeToFile = false; private bool writeToFile = true;
private bool fileExist = true;
private static SysTime time; private static SysTime time;
// Target output // Target output
enum {SYSLOG, STDOUT} enum {
SYSLOG = 1,
STDOUT = 2,
FILE = 4
}
// Message output level // Message output level
enum { enum {
@ -68,33 +74,34 @@ class Log
{ {
if (this.msgLevel > msgLevel) if (this.msgLevel > msgLevel)
return; return;
if (this.msgOutput == STDOUT) if (this.msgOutput & 1)
writeln(message);
else if (this.msgOutput == SYSLOG)
syslog(priority, (message ~ "\0").ptr); syslog(priority, (message ~ "\0").ptr);
if (this.msgOutput & 2)
writeln(message);
if (this.msgOutput & 4)
writeFile(message); writeFile(message);
} }
private void writeFile(string message) private void writeFile(string message)
{ {
if (!this.writeToFile || !this.fileExist) if (!this.writeToFile)
return; return;
if (this.path.exists) if (this.path.exists)
this.fileExist = true; this.writeToFile = true;
else else
{ {
this.fileExist = false; this.writeToFile = false;
this.warning("The log file does not exist: " ~ this.path); this.warning("The log file does not exist: " ~ this.path);
this.fileExist = true;
} }
File file; File file;
try { try {
file = File(this.path, "a+"); file = File(this.path, "a+");
this.writeToFile = true;
} catch (Exception e) { } catch (Exception e) {
this.fileOff(); this.writeToFile = false;
this.error("Unable to open the log file " ~ this.path); this.error("Unable to open the log file " ~ this.path);
this.critical(e); this.critical(e);
return; return;
@ -103,7 +110,7 @@ class Log
try { try {
file.writeln(this.time.format("%Y.%m.%d %H:%M:%S: ") ~ message); file.writeln(this.time.format("%Y.%m.%d %H:%M:%S: ") ~ message);
} catch (Exception e) { } catch (Exception e) {
this.fileOff(); this.writeToFile = false;
this.error("Unable to write to the log file " ~ this.path); this.error("Unable to write to the log file " ~ this.path);
this.critical(e); this.critical(e);
return; return;
@ -112,7 +119,7 @@ class Log
try { try {
file.close(); file.close();
} catch (Exception e) { } catch (Exception e) {
this.fileOff(); this.writeToFile = false;
this.error("Unable to close the log file " ~ this.path); this.error("Unable to close the log file " ~ this.path);
this.critical(e); this.critical(e);
return; return;
@ -134,9 +141,6 @@ class Log
void level(int msgLevel) { this.msgLevel = msgLevel; } void level(int msgLevel) { this.msgLevel = msgLevel; }
void file(string path) { this.path = path; } 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 alert(T)(T message) { writeLog(message.to!string, ALERT, LOG_ALERT); }
void critical(T)(T message) { writeLog(message.to!string, CRIT, LOG_CRIT); } void critical(T)(T message) { writeLog(message.to!string, CRIT, LOG_CRIT); }
void error(T)(T message) { writeLog(message.to!string, ERR, LOG_ERR); } void error(T)(T message) { writeLog(message.to!string, ERR, LOG_ERR); }