Some logging refactoring.

Fixes #14.
master
Stanislav Nikitin 3 years ago
parent 5f58741159
commit 825fd724ff
No known key found for this signature in database
GPG Key ID: 106900B32F8192EE

@ -142,7 +142,7 @@ func pastePOSTWebInterface(ec echo.Context) error {
}
newPasteIDAsString := strconv.FormatInt(id, 10)
c.Logger.Debug().Msgf("Paste saved, URL: /paste/" + newPasteIDAsString)
c.Logger.Debug().Msg("Paste saved, URL: /paste/" + newPasteIDAsString)
// Private pastes have it's timestamp in URL.
if paste.Private {

@ -60,7 +60,7 @@ func pastesGET(ec echo.Context) error {
// Get pastes IDs.
pastes, err3 := c.Database.GetPagedPastes(page)
c.Logger.Debug().Msgf("Got %d pastes", len(pastes))
c.Logger.Debug().Int("count", len(pastes)).Msg("Got pastes")
var pastesString = "No pastes to show."
@ -102,7 +102,7 @@ func pastesGET(ec echo.Context) error {
// Pagination.
pages := c.Database.GetPastesPages()
c.Logger.Debug().Msgf("Total pages: %d, current: %d", pages, page)
c.Logger.Debug().Int("total pages", pages).Int("current page", page).Msg("Paging data")
paginationHTML := pagination.CreateHTML(page, pages, "/pastes/")
pasteListTpl := templater.GetTemplate(ec, "pastelist_list.html", map[string]string{"pastes": pastesString, "pagination": paginationHTML})

@ -99,23 +99,23 @@ func (c *Context) LoadConfiguration() {
// Normalize file path.
normalizedConfigPath, err1 := filepath.Abs(configPath)
if err1 != nil {
c.Logger.Fatal().Msgf("Failed to normalize path to configuration file: %s", err1.Error())
c.Logger.Fatal().Err(err1).Msg("Failed to normalize path to configuration file")
}
c.Logger.Debug().Msgf("Configuration file path: %s", configPath)
c.Logger.Debug().Str("path", configPath).Msg("Configuration file path")
c.Config = &config.Struct{}
// Read configuration file.
fileData, err2 := ioutil.ReadFile(normalizedConfigPath)
if err2 != nil {
c.Logger.Panic().Msgf("Failed to read configuration file: %s", err2.Error())
c.Logger.Panic().Err(err2).Msg("Failed to read configuration file")
}
// Parse it into structure.
err3 := yaml.Unmarshal(fileData, c.Config)
if err3 != nil {
c.Logger.Panic().Msgf("Failed to parse configuration file: %s", err3.Error())
c.Logger.Panic().Err(err3).Msg("Failed to parse configuration file")
}
// Yay! See what it gets!

@ -26,7 +26,7 @@ func (c *Context) initializeHTTPServer() {
go func() {
c.Echo.Logger.Fatal(c.Echo.Start(listenAddress))
}()
c.Logger.Info().Msgf("Started HTTP server at %s", listenAddress)
c.Logger.Info().Str("address", listenAddress).Msg("Started HTTP server")
}
// Wrapper around previous function.

@ -75,7 +75,7 @@ func (db *Database) Initialize() {
} else if c.Config.Database.Type == "postgresql" {
postgresql.New(c)
} else {
c.Logger.Fatal().Msgf("Unknown database type: %s", c.Config.Database.Type)
c.Logger.Fatal().Str("type", c.Config.Database.Type).Msg("Unknown database type")
}
}

@ -53,23 +53,23 @@ func (ff *FlatFiles) GetDatabaseConnection() *sql.DB {
func (ff *FlatFiles) GetPaste(pasteID int) (*structs.Paste, error) {
ff.writeMutex.Lock()
pastePath := filepath.Join(ff.path, "pastes", strconv.Itoa(pasteID)+".json")
c.Logger.Debug().Msgf("Trying to load paste data from '%s'...", pastePath)
c.Logger.Debug().Str("path", pastePath).Msg("Trying to load paste data")
pasteInBytes, err := ioutil.ReadFile(pastePath)
if err != nil {
c.Logger.Debug().Msgf("Failed to read paste from storage: %s", err.Error())
c.Logger.Debug().Err(err).Msg("Failed to read paste from storage")
return nil, err
}
c.Logger.Debug().Msgf("Loaded %d bytes: %s", len(pasteInBytes), string(pasteInBytes))
c.Logger.Debug().Int("paste bytes", len(pasteInBytes)).Msg("Loaded paste")
ff.writeMutex.Unlock()
paste := &structs.Paste{}
err = json.Unmarshal(pasteInBytes, paste)
if err != nil {
c.Logger.Error().Msgf("Failed to parse paste: %s", err.Error())
return nil, err
err1 := json.Unmarshal(pasteInBytes, paste)
if err1 != nil {
c.Logger.Error().Err(err1).Msgf("Failed to parse paste")
return nil, err1
}
return paste, nil
@ -82,8 +82,6 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
startPagination = (page - 1) * c.Config.Pastes.Pagination
}
c.Logger.Debug().Msgf("Pastes index: %+v", ff.pastesIndex)
// Iteration one - get only public pastes.
var publicPastes []*Index
@ -93,8 +91,6 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
}
}
c.Logger.Debug().Msgf("%+v", publicPastes)
// Iteration two - get paginated pastes.
pastesData := make([]structs.Paste, 0)
@ -104,29 +100,29 @@ func (ff *FlatFiles) GetPagedPastes(page int) ([]structs.Paste, error) {
}
if idx < startPagination {
c.Logger.Debug().Msgf("Paste with index %d isn't in pagination query: too low index", idx)
c.Logger.Debug().Int("paste index", idx).Msg("Paste isn't in pagination query: too low index")
continue
}
if (idx-1 >= startPagination && page > 1 && idx > startPagination+((page-1)*c.Config.Pastes.Pagination)) || (idx-1 >= startPagination && page == 1 && idx > startPagination+(page*c.Config.Pastes.Pagination)) {
c.Logger.Debug().Msgf("Paste with index %d isn't in pagination query: too high index", idx)
c.Logger.Debug().Int("paste index", idx).Msg("Paste isn't in pagination query: too high index")
break
}
c.Logger.Debug().Msgf("Getting paste data (ID: %d, index: %d)", paste.ID, idx)
c.Logger.Debug().Int("ID", paste.ID).Int("index", idx).Msg("Getting paste data")
// Get paste data.
pasteData := &structs.Paste{}
pasteRawData, err := ioutil.ReadFile(filepath.Join(ff.path, "pastes", strconv.Itoa(paste.ID)+".json"))
if err != nil {
c.Logger.Error().Msgf("Failed to read paste data: %s", err.Error())
c.Logger.Error().Err(err).Msg("Failed to read paste data")
continue
}
err = json.Unmarshal(pasteRawData, pasteData)
if err != nil {
c.Logger.Error().Msgf("Failed to parse paste data: %s", err.Error())
err1 := json.Unmarshal(pasteRawData, pasteData)
if err1 != nil {
c.Logger.Error().Err(err1).Msg("Failed to parse paste data")
continue
}
@ -181,18 +177,18 @@ func (ff *FlatFiles) Initialize() {
// Create directory if necessary.
if _, err := os.Stat(ff.path); err != nil {
c.Logger.Debug().Msgf("Directory '%s' does not exist, creating...", ff.path)
c.Logger.Debug().Str("directory", ff.path).Msg("Directory does not exist, creating...")
_ = os.MkdirAll(ff.path, os.ModePerm)
} else {
c.Logger.Debug().Msgf("Directory '%s' already exists", ff.path)
c.Logger.Debug().Str("directory", ff.path).Msg("Directory already exists")
}
// Create directory for pastes.
if _, err := os.Stat(filepath.Join(ff.path, "pastes")); err != nil {
c.Logger.Debug().Msgf("Directory '%s' does not exist, creating...", filepath.Join(ff.path, "pastes"))
c.Logger.Debug().Str("directory", ff.path).Msg("Directory does not exist, creating...")
_ = os.MkdirAll(filepath.Join(ff.path, "pastes"), os.ModePerm)
} else {
c.Logger.Debug().Msgf("Directory '%s' already exists", filepath.Join(ff.path, "pastes"))
c.Logger.Debug().Str("directory", ff.path).Msg("Directory already exists")
}
// Load pastes index.
@ -205,12 +201,12 @@ func (ff *FlatFiles) Initialize() {
c.Logger.Fatal().Msg("Failed to read contents of index file!")
}
err = json.Unmarshal(indexData, &ff.pastesIndex)
if err != nil {
c.Logger.Error().Msgf("Failed to parse index file contents from JSON into internal structure. Will create new index file. All of your previous pastes will became unavailable. Error was: %s", err.Error())
err1 := json.Unmarshal(indexData, &ff.pastesIndex)
if err1 != nil {
c.Logger.Error().Err(err1).Msg("Failed to parse index file contents from JSON into internal structure. Will create new index file. All of your previous pastes will became unavailable.")
}
c.Logger.Debug().Msgf("Parsed pastes index: %+v", ff.pastesIndex)
c.Logger.Debug().Int("pastes count", len(ff.pastesIndex)).Msg("Parsed pastes index")
}
}
@ -221,7 +217,7 @@ func (ff *FlatFiles) SavePaste(p *structs.Paste) (int64, error) {
pasteID := len(filesOnDisk) + 1
p.ID = pasteID
c.Logger.Debug().Msgf("Writing paste to disk, ID will be " + strconv.Itoa(pasteID))
c.Logger.Debug().Int("new paste ID", pasteID).Msg("Writing paste to disk")
data, err := json.Marshal(p)
if err != nil {
@ -250,13 +246,13 @@ func (ff *FlatFiles) Shutdown() {
indexData, err := json.Marshal(ff.pastesIndex)
if err != nil {
c.Logger.Error().Msgf("Failed to encode index data into JSON: %s", err.Error())
c.Logger.Error().Err(err).Msg("Failed to encode index data into JSON")
return
}
err = ioutil.WriteFile(filepath.Join(ff.path, "pastes", "index.json"), indexData, 0644)
if err != nil {
c.Logger.Error().Msgf("Failed to write index data to file. Pretty sure that you've lost your pastes.")
err1 := ioutil.WriteFile(filepath.Join(ff.path, "pastes", "index.json"), indexData, 0644)
if err1 != nil {
c.Logger.Error().Err(err1).Msg("Failed to write index data to file. Pretty sure that you've lost your pastes.")
return
}
}

@ -152,11 +152,11 @@ func (db *Database) Initialize() {
}
dbConnString := fmt.Sprintf("%s@tcp(%s:%s)/%s?parseTime=true&collation=utf8mb4_unicode_ci&charset=utf8mb4", userpass, c.Config.Database.Address, c.Config.Database.Port, c.Config.Database.Database)
c.Logger.Debug().Msgf("Database connection string: %s", dbConnString)
c.Logger.Debug().Str("DSN", dbConnString).Msgf("Database connection string composed")
dbConn, err := sqlx.Connect("mysql", dbConnString)
if err != nil {
c.Logger.Error().Msgf("Failed to connect to database: %s", err.Error())
c.Logger.Error().Err(err).Msg("Failed to connect to database")
return
}
@ -192,7 +192,7 @@ func (db *Database) Shutdown() {
if db.db != nil {
err := db.db.Close()
if err != nil {
c.Logger.Error().Msgf("Failed to close database connection: %s", err.Error())
c.Logger.Error().Err(err).Msg("Failed to close database connection")
}
}
}

@ -165,11 +165,11 @@ func (db *Database) Initialize() {
}
dbConnString := fmt.Sprintf("postgres://%s@%s:%s/%s?connect_timeout=10&fallback_application_name=fastpastebin&sslmode=disable", userpass, c.Config.Database.Address, c.Config.Database.Port, c.Config.Database.Database)
c.Logger.Debug().Msgf("Database connection string: %s", dbConnString)
c.Logger.Debug().Str("DSN", dbConnString).Msg("Database connection string composed")
dbConn, err := sqlx.Connect("postgres", dbConnString)
if err != nil {
c.Logger.Error().Msgf("Failed to connect to database: %s", err.Error())
c.Logger.Error().Err(err).Msg("Failed to connect to database")
return
}
@ -204,7 +204,7 @@ func (db *Database) Shutdown() {
if db.db != nil {
err := db.db.Close()
if err != nil {
c.Logger.Error().Msgf("Failed to close database connection: %s", err.Error())
c.Logger.Error().Err(err).Msg("Failed to close database connection")
}
}
}

@ -73,7 +73,7 @@ func GetRawTemplate(ec echo.Context, templateName string, data map[string]string
// GetTemplate returns formatted template that can be outputted to client.
func GetTemplate(ec echo.Context, name string, data map[string]string) string {
log.Debug().Msgf("Requested template '%s'", name)
log.Debug().Str("name", name).Msg("Requested template")
// Getting main template.
mainhtml, err := static.ReadFile("main.html")

Loading…
Cancel
Save