-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Umask call to the main functin in order to prevent Umask from …
…modifying folder access when creating logging folders. Adding AuthLogger array to AuthController to allow the user to add Loggers to the server. Added AddLogger method to add a logger object. Moved all instances of AuthController methods to use pointer-to-AuthController instead of a copy. Added and updated commentary. Updated configureReleaseLogging to be addLogging. addLogging goes through all logger instances and adds a log to each instance. Adding configureReleaseLogging to add logging to the app that writes to MongoDB Re-factored logging functions to a go into the authUtils folder/package. Added LogData interface, RequestLogData and InfoLogData that implement LogData, LoggingError, AuthLogger, FileLogger, ConsoleLogger, which implement AuthLogger Updated DatabaseController to implement AuthLogger interface. Update MongoDbController to use pointer-to MongoDbController instead of a copy. renamed timeStamp and TimeStamp to timestamp and Timestamp. Updated TestDbController to use all update functions from DatabaseController.
- Loading branch information
Mathew Thompson
authored and
Mathew Thompson
committed
Aug 9, 2021
1 parent
860367a
commit 48e7ef6
Showing
8 changed files
with
293 additions
and
109 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
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,164 @@ | ||
package authUtils | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
) | ||
|
||
/**************************************************************************************** | ||
* LoggingError | ||
****************************************************************************************/ | ||
type LoggingError struct{ ErrMsg string } | ||
|
||
func (err LoggingError) Error() string { return err.ErrMsg } | ||
func NewLoggingError(msg string) error { return LoggingError{msg} } | ||
|
||
/**************************************************************************************** | ||
* LogData | ||
****************************************************************************************/ | ||
type LogData interface { | ||
PrettyString() string | ||
} | ||
|
||
/**************************************************************************************** | ||
* RequestLogData | ||
****************************************************************************************/ | ||
type RequestLogData struct { | ||
Timestamp time.Time `bson:"timestamp"` | ||
Type string `bson:"type"` | ||
ClientIP string `bson:"clientIP"` | ||
Method string `bson:"method"` | ||
Path string `bson:"path"` | ||
Protocol string `bson:"protocol"` | ||
StatusCode int `bson:"statusCode"` | ||
Latency time.Duration `bson:"latency"` | ||
UserAgent string `bson:"userAgent"` | ||
ErrorMessage string `bson:"errorMessage"` | ||
} | ||
|
||
func (rld RequestLogData) PrettyString() string { | ||
msg := fmt.Sprintf("%s - [%s] %s %s %s %d %s \"%s\" \"%s\"", | ||
rld.Timestamp.Format(time.RFC1123), | ||
rld.ClientIP, | ||
rld.Method, | ||
rld.Path, | ||
rld.Protocol, | ||
rld.StatusCode, | ||
rld.Latency, | ||
rld.UserAgent, | ||
rld.ErrorMessage, | ||
) | ||
|
||
return msg | ||
} | ||
|
||
/**************************************************************************************** | ||
* InfoLogData | ||
****************************************************************************************/ | ||
type InfoLogData struct { | ||
Timestamp time.Time `bson:"timestamp"` | ||
Type string `bson:"type"` | ||
Message string `bson:"message"` | ||
} | ||
|
||
func (ild InfoLogData) PrettyString() string { | ||
msg := fmt.Sprintf("%s - [%s] \"%s\"", | ||
ild.Timestamp.Format(time.RFC1123), | ||
ild.Type, | ||
ild.Message, | ||
) | ||
|
||
return msg | ||
} | ||
|
||
/**************************************************************************************** | ||
* AuthLogger | ||
****************************************************************************************/ | ||
type AuthLogger interface { | ||
AddRequestLog(log *RequestLogData) error | ||
AddInfoLog(log *InfoLogData) error | ||
} | ||
|
||
/**************************************************************************************** | ||
* FileLogger | ||
****************************************************************************************/ | ||
type FileLogger struct { | ||
FilePath string | ||
FileName string | ||
FileHandle *os.File | ||
} | ||
|
||
func (fl *FileLogger) AddRequestLog(log *RequestLogData) error { | ||
err := fl.WriteLog(log) | ||
|
||
return err | ||
} | ||
|
||
func (fl *FileLogger) AddInfoLog(log *InfoLogData) error { | ||
err := fl.WriteLog(log) | ||
|
||
return err | ||
} | ||
|
||
func (fl *FileLogger) WriteLog(log LogData) error { | ||
if fl.FileHandle == nil { | ||
return NewLoggingError("fileHandle is nil (no file handle exists)") | ||
} | ||
|
||
_, err := fl.FileHandle.WriteString(log.PrettyString() + "\n") | ||
|
||
return err | ||
} | ||
|
||
func MakeNewFileLogger(path string, name string) *FileLogger { | ||
print("Making New File Logger \n") | ||
fl := FileLogger{ | ||
FileName: name, | ||
FilePath: path, | ||
} | ||
|
||
var handle *os.File | ||
var handleErr error | ||
fullPath := filepath.Join(path, name) | ||
|
||
var pathErr error | ||
|
||
if _, err := os.Stat(path); os.IsNotExist(err) { | ||
pathErr = os.MkdirAll(path, 0764) | ||
} | ||
|
||
// We return the FileLogger with FileHandle set to nil | ||
if pathErr != nil { | ||
// do something | ||
return &fl | ||
} | ||
|
||
// file, _ := os.Create("gin.log") | ||
handle, handleErr = os.OpenFile(fullPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
|
||
fmt.Println(handleErr) | ||
|
||
fl.FileHandle = handle | ||
|
||
fmt.Println(fl.FileHandle == nil) | ||
|
||
return &fl | ||
} | ||
|
||
/**************************************************************************************** | ||
* ConsoleLogger | ||
****************************************************************************************/ | ||
type ConsoleLogger struct { | ||
LogPath string | ||
FileName string | ||
} | ||
|
||
func (cl ConsoleLogger) AddRequestLog(log RequestLogData) error { | ||
return nil | ||
} | ||
|
||
func (cl ConsoleLogger) AddInfoLog(log *InfoLogData) error { | ||
return nil | ||
} |
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
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
Oops, something went wrong.