Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new post: Escrevendo logs para um arquivo customizado
- Loading branch information
Showing
4 changed files
with
46 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
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 @@ | ||
module github.com/aprendagolang/log | ||
|
||
go 1.16 |
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,30 @@ | ||
package logs | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
// Write escreve uma mensagem de log em um arquivo | ||
func Write(message, filepath string) { | ||
// define um caminho se o filepath não for passado | ||
if filepath == "" { | ||
filepath = "/tmp/my-app.log" | ||
} | ||
|
||
// abre o arquivo onde vamos escrever nossa mensagem | ||
file, err := os.OpenFile(filepath, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
defer file.Close() | ||
|
||
// seta a saída do log para o arquivo que abrimos | ||
log.SetOutput(file) | ||
|
||
// loga data e hora | ||
log.SetFlags(log.LstdFlags) | ||
|
||
// escreve a mensagem | ||
log.Println(message) | ||
} |
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,12 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/aprendagolang/log/logs" | ||
) | ||
|
||
func main() { | ||
// escreve no log ola.log | ||
logs.Write("olá mundo!", "./ola.log") | ||
// escreve no log /tmp/my-app.log | ||
logs.Write("funciona!!!", "") | ||
} |