Skip to content

Commit

Permalink
Aplica melhorias.
Browse files Browse the repository at this point in the history
  • Loading branch information
armando-couto committed Mar 17, 2022
1 parent ff273c8 commit 32281aa
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 72 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ ConvertIntToString(value int) Return string
### logger_file.go

```go
CreateFileDayInfo(message string) Return void
CreateFileDayError(message interface{}) Return void
CreateFileDay(message interface{}) Return void
```

### mysql_connection.go
Expand Down
14 changes: 7 additions & 7 deletions aws_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func UpdloadInS3(file multipart.File, path, fileName string) string {
Body: file,
})
if err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: err.Error()}))
CreateFileDay(FormatMessage(Message{Error: err.Error()}))
return ""
}
CreateFileDayInfo(fmt.Sprint("Upload do Arquivo: ", up.UploadID))
CreateFileDay(FormatMessage(Message{Info: fmt.Sprint("Upload do Arquivo: ", up.UploadID)}))
return "https://" + myBucket + "." + "s3.amazonaws.com/" + fileName
}

Expand All @@ -60,7 +60,7 @@ func UpdloadInS3Base64(b64 string, path, fileName string) string {

dec, err := base64.StdEncoding.DecodeString(b64)
if err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: err.Error()}))
CreateFileDay(FormatMessage(Message{Error: err.Error()}))
return ""
}
//file, header, err := c.Request.FormFile("photo")
Expand All @@ -72,10 +72,10 @@ func UpdloadInS3Base64(b64 string, path, fileName string) string {
Body: bytes.NewBuffer(dec),
})
if err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: err.Error()}))
CreateFileDay(FormatMessage(Message{Error: err.Error()}))
return ""
}
CreateFileDayInfo(fmt.Sprint("Upload do Arquivo: ", up.UploadID))
CreateFileDay(FormatMessage(Message{Info: fmt.Sprint("Upload do Arquivo: ", up.UploadID)}))
return "https://" + myBucket + "." + "s3.amazonaws.com/" + fileName
}

Expand Down Expand Up @@ -103,9 +103,9 @@ func UpdloadInS3Base64Byte(b64 []byte, path, fileName string) string {
Body: bytes.NewBuffer(b64),
})
if err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: err.Error()}))
CreateFileDay(FormatMessage(Message{Error: err.Error()}))
return ""
}
CreateFileDayInfo(fmt.Sprint("Upload do Arquivo: ", up.UploadID))
CreateFileDay(FormatMessage(Message{Info: fmt.Sprint("Upload do Arquivo: ", up.UploadID)}))
return "https://" + myBucket + "." + "s3.amazonaws.com/" + fileName
}
2 changes: 1 addition & 1 deletion base64.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func EncodeStringToBase64(value string) string {
func DecodeBase64ToString(value string) string {
data, err := base64.StdEncoding.DecodeString(value)
if err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: err.Error()}))
CreateFileDay(FormatMessage(Message{Error: err.Error()}))
return "ERRONOJSON"
}
return string(data)
Expand Down
4 changes: 2 additions & 2 deletions connection_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
func ConnectionBDMySQL() *sql.DB {
db, err := sql.Open("mysql", Godotenv("database_mysql"))
if err != nil {
CreateFileDayError(MessageError{File: "ConnectionBDMySQL()", Error: err.Error()})
CreateFileDay(Message{File: "ConnectionBDMySQL()", Error: err.Error()})
if db.Ping() != nil {
CreateFileDayError(MessageError{File: "ConnectionBDMySQL()", Error: "Erro ping DB"})
CreateFileDay(Message{File: "ConnectionBDMySQL()", Error: "Erro ping DB"})
}
}

Expand Down
7 changes: 3 additions & 4 deletions csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func CSVGZExport(StructCsv [][]string, nomeArquivo string) error {
defer wgzp.Close()
}

CreateFileDayInfo(fmt.Sprint("Arquivo ", nomeArquivo, " gerado CSV.GZ"))

CreateFileDay(FormatMessage(Message{Info: fmt.Sprint("Arquivo ", nomeArquivo, " gerado CSV.GZ")}))
return nil
}

Expand All @@ -51,15 +50,15 @@ func CSVGZExport(StructCsv [][]string, nomeArquivo string) error {
func CSVRead(pathFile string) [][]string {
f, err := os.Open(pathFile)
if err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: "ERRO: Leitura arquivo CSV:" + err.Error()}))
CreateFileDay(FormatMessage(Message{Error: "ERRO: Leitura arquivo CSV:" + err.Error()}))
}
defer f.Close()

arqReader := csv.NewReader(f)
arqReader.LazyQuotes = true
lines, err := arqReader.ReadAll()
if err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: "ERRO: Leitura arquivo CSV:" + err.Error()}))
CreateFileDay(FormatMessage(Message{Error: "ERRO: Leitura arquivo CSV:" + err.Error()}))
}
return lines
}
Expand Down
12 changes: 6 additions & 6 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func ListFolderFiles(path string) []os.FileInfo {
files, err := ioutil.ReadDir(path)
if err != nil {
CreateFileDayError(FormatMessage(MessageError{File: "ListFolderFiles(path)", Error: fmt.Sprint("a pasta '", path, "' não existe!")}))
CreateFileDay(FormatMessage(Message{File: "ListFolderFiles(path)", Error: fmt.Sprint("a pasta '", path, "' não existe!")}))
}
return files
}
Expand All @@ -27,7 +27,7 @@ func ReadingFiles(path string, fileName string) string {
file, err := os.Open(fmt.Sprint(path, fileName))
if err != nil {
log.Fatal(err)
CreateFileDayError(FormatMessage(MessageError{File: "ReadingFiles(path, fileName)", Error: fmt.Sprint("O arquivo '", fileName, "' não está mais na pasta!")}))
CreateFileDay(FormatMessage(Message{File: "ReadingFiles(path, fileName)", Error: fmt.Sprint("O arquivo '", fileName, "' não está mais na pasta!")}))
}
defer file.Close()

Expand All @@ -40,9 +40,9 @@ func ReadingFiles(path string, fileName string) string {
*/
func RemoveFilesOfPath(path string) {
if os.Remove(path) != nil {
CreateFileDayError(FormatMessage(MessageError{Error: "Problema ao remover do diretório local"}))
CreateFileDay(FormatMessage(Message{Error: "Problema ao remover do diretório local"}))
} else {
CreateFileDayError(FormatMessage(MessageError{Error: "Removido do local"}))
CreateFileDay(FormatMessage(Message{Error: "Removido do local"}))
}
}

Expand Down Expand Up @@ -81,7 +81,7 @@ func CreateArchiveNodirectory(nomeDoArquivo string, conteudo string) error {
*/
func IsError(err error) bool {
if err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: err.Error()}))
CreateFileDay(FormatMessage(Message{Error: err.Error()}))
}
return err != nil
}
Expand All @@ -98,5 +98,5 @@ func Backup(path, name, texto string) {

path_nome_arquivo_antigo := fmt.Sprint(path, name)
os.Remove(path_nome_arquivo_antigo)
CreateFileDayInfo(fmt.Sprint("Concluída a Leitura do Arquivo: ", name))
CreateFileDay(FormatMessage(Message{Info: fmt.Sprint("Concluída a Leitura do Arquivo: ", name)}))
}
6 changes: 3 additions & 3 deletions ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ import (
func ListOfAvailableFilesNoFTP(remote string, port string, user string, pass string) (*ftp.ServerConn, error, []string) {
c, err := ftp.Dial(remote+":"+port, ftp.DialWithTimeout(60*time.Second))
if err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: err.Error()}))
CreateFileDay(FormatMessage(Message{Error: err.Error()}))
}
err = c.Login(user, pass)
if err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: err.Error()}))
CreateFileDay(FormatMessage(Message{Error: err.Error()}))
}

//var nome_arquivo string
list, _ := c.NameList("/")
if err := c.Quit(); err != nil {
CreateFileDayError(FormatMessage(MessageError{Error: err.Error()}))
CreateFileDay(FormatMessage(Message{Error: err.Error()}))
}
return c, err, list
}
Expand Down
58 changes: 11 additions & 47 deletions logger_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,28 @@ import (
"time"
)

type MongoObject struct {
Date time.Time
Message string
}

/*
CreateFileDayInfo o antigo nome era: CreateFileDay
*/
func CreateFileDayInfo(message string) {
message = strings.ReplaceAll(message, "\n", "")
if ConvertStringToBool(Godotenv("logger")) {
connection := ConnectionMongoDB()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Second)
defer cancel()
defer connection.Client().Disconnect(ctx)
_, err := connection.Collection("infos").InsertOne(ctx, MongoObject{Date: time.Now(), Message: message})
if err != nil {
log.Println(err)
}
} else {
f, err := os.OpenFile(fmt.Sprint(time.Now().Format("20060102"), ".log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()

logger := log.New(f, "INFO\t", log.Ldate|log.Ltime)
logger.Println(message)
fmt.Println(message)
}
}

type MessageError struct {
type Message struct {
Log *log.Logger
File string
Query string
Info string
Error string
Objects interface{}
EnvDev bool
}

func CreateFileDayError(message MessageError) {
func CreateFileDay(message Message) {
message.Query = strings.ReplaceAll(message.Query, "\n", "")
// Se for FALSE não iremos imprimir dentro do arquivo de LOG que é versionado
if !message.EnvDev {
fmt.Println(message)
} else {
f, err := os.OpenFile(fmt.Sprint(time.Now().Format("20060102"), ".log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()

message.Log.Println(message)
f, err := os.OpenFile(fmt.Sprint(time.Now().Format("20060102"), ".log"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()

message.Log.Println(message)
fmt.Println(message)
}

func FormatMessage(message MessageError) MessageError {
func FormatMessage(message Message) Message {
message.Log.SetFlags(log.LstdFlags | log.Lshortfile)
return message
}
Expand Down

0 comments on commit 32281aa

Please sign in to comment.