-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 144f171
Showing
6 changed files
with
482 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
flog.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Flog | ||
> A simple file logger written in [Golang](https://golang.org/). | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) | ||
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/hackmac89/flog/graphs/commit-activity) | ||
[![Golint code style](https://img.shields.io/badge/code_style-Golint-CFB69A.svg)](https://github.com/golang/lint) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/golang/crypto)](https://goreportcard.com/report/github.com/hackmac89/flog) | ||
[![codecov.io Code Coverage](https://img.shields.io/codecov/c/github/hackmac89/flog.svg?maxAge=2592000)](https://codecov.io/github/hackmac89/flog?branch=master) | ||
[![Github all releases](https://img.shields.io/github/downloads/hackmac89/go-share/total.svg)](https://github.com/hackmac89/flog/releases/) | ||
|
||
## Usage | ||
|
||
Import package to your project. | ||
|
||
Instantiate a new logger with a given filepath, where the logs shall be written to. | ||
|
||
```go | ||
logger, logErr := flog.NewFileLogger("test.log") | ||
``` | ||
|
||
Call the appropriate function when logging **INFO**, **DEBUG**, **WARNING** or **ERROR** messages | ||
|
||
```go | ||
// print INFO message | ||
logger.PrintInfo("Printing \"%s\" message", "INFO") | ||
|
||
// print DEBUG message | ||
logger.PrintDebug("Printing \"%s\" message", "DEBUG") | ||
|
||
// print WARNING message | ||
logger.PrintWarning("Printing \"%s\" message", "WARNING") | ||
|
||
// print ERROR message | ||
logger.PrintError("Printing \"%s\" message", "ERRROR") | ||
``` | ||
|
||
### Example output | ||
|
||
![example](https://raw.githubusercontent.com/hackmac89/flog/master/example.png) | ||
|
||
## Release History | ||
|
||
* 1.0.0 (05/20/19) | ||
* Initial commit/release | ||
|
||
## Versioning | ||
|
||
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/your/project/tags). | ||
|
||
## Authors | ||
|
||
* **hackmac89** - *Initial work* - [hackmac89](https://github.com/hackmac89) | ||
|
||
See also the list of [contributors](https://github.com/hackmac89/go-share/contributors) who participated in this project. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details | ||
|
||
## Meta | ||
|
||
hackmac89 – [@hackmac89](https://twitter.com/hackmac89) – [email protected] – [https://github.com/hackmac89/](https://github.com/hackmac89/) | ||
|
||
## Contributing | ||
|
||
1. Fork it (<https://github.com/hackmac89/flog/fork>) | ||
2. Create your feature branch (`git checkout -b feature/featurename`) | ||
3. Commit your changes (`git commit -am 'Added some feature'`) | ||
4. Push to the branch (`git push origin feature/featurename`) | ||
5. Create a new Pull Request |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
Package flog provides the ability to do file logging (hence the name) | ||
File: flog.go | ||
Version: 2.0 | ||
Synopsis: provides file logging for the other packages | ||
Author: Jeff Wagner | ||
E-mail: [email protected] | ||
date: 05/13/2019 | ||
last modification date: 05/14/2019 | ||
*/ | ||
|
||
//Package flog provides the ability to do file logging (hence the name) | ||
package flog | ||
|
||
//Importe | ||
import ( | ||
"errors" | ||
f "fmt" | ||
"os" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var mutex sync.Mutex | ||
|
||
type loggingLevel uint | ||
|
||
const ( | ||
_Info loggingLevel = 1 << iota | ||
_Debug | ||
_Warning | ||
_Error | ||
) | ||
|
||
//FileLogger ... | ||
type FileLogger interface { | ||
PrintInfo(formatString string, args ...interface{}) (int, error) | ||
PrintDebug(formatString string, args ...interface{}) (int, error) | ||
PrintWarning(formatString string, args ...interface{}) (int, error) | ||
PrintError(formatString string, args ...interface{}) (int, error) | ||
} | ||
|
||
//logger ... | ||
type logger struct { | ||
level loggingLevel | ||
fPath string | ||
fHandle *os.File | ||
} | ||
|
||
//NewFileLogger returns a pointer to our logger | ||
func NewFileLogger(filePath string) (FileLogger, error) { | ||
if len(filePath) > 0 { | ||
return &logger{_Info, filePath, nil}, nil | ||
} | ||
return nil, errors.New("ERROR(flog.go -> NewFileLogger): No path was given") | ||
} | ||
|
||
//PrintToLog writes a given message and a timestamp into a logfile (I´m usually not checking for errors when calling this function, so errors are quite silent) | ||
func (l *logger) printToLog(formatString string, args ...interface{}) (int, error) { | ||
// Declarations | ||
var bytesWritten int | ||
var handleErr error | ||
var level string | ||
|
||
mutex.Lock() // lock access | ||
defer mutex.Unlock() | ||
t := time.Now() | ||
switch l.level { | ||
case _Info: | ||
level = "INFO" | ||
case _Debug: | ||
level = "DEBUG" | ||
case _Warning: | ||
level = "WARNING" | ||
case _Error: | ||
level = "ERROR" | ||
default: | ||
level = "INFO" | ||
} | ||
timestamp := f.Sprintf("[%02d.%02d.%d %02d:%02d:%02d]: %s ", t.Day(), t.Month(), t.Year(), t.Hour(), t.Minute(), t.Second(), level) | ||
// Re-obtain file fHandle | ||
l.fHandle, handleErr = openHandle(l.fPath) | ||
if handleErr != nil { | ||
return 0, handleErr | ||
} | ||
|
||
defer l.fHandle.Close() // defer closing of the file handler | ||
// Write timestamp and message to file | ||
bytesWritten, err := l.fHandle.WriteString(f.Sprintf(timestamp+formatString+"\n", args...)) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return bytesWritten, nil | ||
} | ||
|
||
//openHandle returns a handle for the opened file or an error otherwise | ||
func openHandle(filePath string) (*os.File, error) { | ||
return os.OpenFile(filePath, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0744) // Use "Append" to append messages to an existing logfile or create a new one if needed | ||
} | ||
|
||
//PrintInfo prints a standard informational message | ||
func (l logger) PrintInfo(formatString string, args ...interface{}) (int, error) { | ||
l.level = _Info | ||
return l.printToLog(formatString, args...) | ||
} | ||
|
||
//PrintDebug prints a debugging message | ||
func (l logger) PrintDebug(formatString string, args ...interface{}) (int, error) { | ||
l.level = _Debug | ||
return l.printToLog(formatString, args...) | ||
} | ||
|
||
//PrintWarning prints a warning message | ||
func (l logger) PrintWarning(formatString string, args ...interface{}) (int, error) { | ||
l.level = _Warning | ||
return l.printToLog(formatString, args...) | ||
} | ||
|
||
//PrintError prints an error message | ||
func (l logger) PrintError(formatString string, args ...interface{}) (int, error) { | ||
l.level = _Error | ||
return l.printToLog(formatString, args...) | ||
} |
Oops, something went wrong.