Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello! At the institution I work for, we use programs in Pro*C (SQL embebbed in C language)
There are multiple servers in the architecture, and problems can occur when trying to know where to look for the log file if there is an error. I am trying to sort the log files because it is like a mess
We should manage some log levels, like: info, warning, error and fatal, and log files should be saves in a directory: /interfaces/logs

These pieces of code use the library
<syslog.h>, and <syslog.c>:
In `ifcp0150.pc`:

	void GrabacionLog(int p_nivel_mensaje,char *p_texto,int p_numero_linea,int p_tipo_mensaje){
		if(p_tipo_mensaje == MENSAJELOG){
			syslog(p_nivel_mensaje, "[%5d] ---- %s ", p_numero_linea, p_texto);
		}
		else{
			syslog(p_nivel_mensaje, "[%5d] ---- %s [Error : %m]",p_numero_linea,p_texto);
		}
	}

	void AperturaLog(char *p_nombre_programa, int p_facilidad){
		openlog(p_nombre_programa, LOG_PID | LOG_NDELAY | LOG_NOWAIT , p_facilidad);
		MensajeLogInformacion("---- Comienzo Ejecucion ----", __LINE__);
	}

The function `GrabacionLog` has some alias, depending of the kind of log:
#define MENSAJELOG      0
#define ERRORLOG        1

#define MensajeLogEmergencia(y,z)       GrabacionLog(LOG_EMERG,y,z,MENSAJELOG)      /* 0 */
#define MensajeLogAlerta(y,z)           GrabacionLog(LOG_ALERT,y,z,MENSAJELOG)      /* 1 */
#define MensajeLogCritico(y,z)          GrabacionLog(LOG_CRIT,y,z,MENSAJELOG)       /* 2 */
#define MensajeLogError(y,z)            GrabacionLog(LOG_ERR,y,z,MENSAJELOG)        /* 3 */
#define MensajeLogAdvertencia(y,z)      GrabacionLog(LOG_WARNING,y,z,MENSAJELOG)    /* 4 */
#define MensajeLogNoticia(y,z)          GrabacionLog(LOG_NOTICE,y,z,MENSAJELOG)     /* 5 */
#define MensajeLogInformacion(y,z)      GrabacionLog(LOG_INFO,y,z,MENSAJELOG)       /* 6 */
#define MensajeLogDebug(y,z)            GrabacionLog(LOG_DEBUG,y,z,MENSAJELOG)      /* 7 */

#define ErrorLogEmergencia(y,z)         GrabacionLog(LOG_EMERG,y,z,ERRORLOG)        /* 0 */
#define ErrorLogAlerta(y,z)             GrabacionLog(LOG_ALERT,y,z,ERRORLOG)        /* 1 */
#define ErrorLogCritico(y,z)            GrabacionLog(LOG_CRIT,y,z,ERRORLOG)         /* 2 */
#define ErrorLogError(y,z)              GrabacionLog(LOG_ERR,y,z,ERRORLOG)          /* 3 */
#define ErrorLogAdvertencia(y,z)        GrabacionLog(LOG_WARNING,y,z,ERRORLOG)      /* 4 */
#define ErrorLogNoticia(y,z)            GrabacionLog(LOG_NOTICE,y,z,ERRORLOG)       /* 5 */
#define ErrorLogInformacion(y,z)        GrabacionLog(LOG_INFO,y,z,ERRORLOG)         /* 6 */
#define ErrorLogDebug(y,z)              GrabacionLog(LOG_DEBUG,y,z,ERRORLOG)        /* 7 */

#define AperturaLogDemonio(y)           AperturaLog(y,LOG_LOCAL7)
#define AperturaLogReportes(y)          AperturaLog(y,LOG_LOCAL6)

In `reportsdaemon.pc`:
int main(int argc, char *argv[]){
    UseSysLogMessage(argv[0]);
    UseSysLogDebug(argv[0]);

In `ifcp0170.c`:
UseSysLogMessage(argv[0]);
UseSysLogDebug(argv[0]);
SetDebugLevel(1);


In `ue_funcion.pc`:
UseSysLogMessage(funcion);


and afterwards:
UseSysLogMessage("ue_maquina_estados.pc");


There are also functions that do not use the syslog library and they just write on a file, like these ones:
void log_error_msg(const char *context_info, char *mensaje, ... ){
    FILE *fp;
    char arch_log[150];
    va_list ap;
    char desc[200];
    char host[50];
    varchar dia_hora[100];
    long  sql_code;
    char l_directorio[100] = "";
    char l_paso[100];
    sql_code = sqlca.sqlcode;
    va_start(ap, mensaje);
    vsprintf(desc,mensaje, ap);
    va_end(ap);
    exec sql
    select to_char(sysdate, 'Mon dd hh24:mi:ss')
    into   :dia_hora
    from dual;
    ora_close(dia_hora);
    step_ind(desc);

    if(getenv("DEBUG_LOG_ACR")!=NULL AND strcmp(getenv("DEBUG_LOG_ACR"),"S")==0){
        PATH_MSG(l_directorio, l_paso);
        strcpy(arch_log, l_paso);
        strcat(arch_log, ARCHIVO_MSG);
        fp=fopen(arch_log, "a");
        fprintf(fp, (char *)dia_hora.arr);
        fprintf(fp, " ");
        if(getenv("SESSION_SVR") != NULL ){
            strcpy(host, getenv("SESSION_SVR"));
            fprintf(fp, host);
            fprintf(fp, " ");
        }
        fprintf(fp, context_info);
        fprintf(fp, " ");
        fprintf(fp, desc);
        fprintf(fp, " ");
        fprintf(fp, "SQLCODE:%ld",sql_code);
        fprintf(fp, "\n");
        fclose(fp);
    }
}

void log_error(char *mensaje, ... ){
    FILE *fp;
    char arch_log[150];
    va_list ap;
    char desc[200];
    char l_directorio[100];
    char l_paso[100];

    va_start(ap, mensaje);
    vsprintf(desc, mensaje, ap);
    va_end(ap);
    step_ind(desc);
    if(getenv("DEBUG_LOG_ACR")!=NULL AND strcmp(getenv("DEBUG_LOG_ACR"),"S")==0){
        PATH(l_directorio, l_paso);
        strcpy(arch_log, l_paso);
        strcat(arch_log, ARCHIVO);
        fp= fopen(arch_log, "a");
        fprintf(fp, desc);
        fprintf(fp, "\n");
        fclose(fp);
    }
}

We would like to unify the logs and use just the `syslog`. Any advice/tip please?

What I have tried:

The log_error_msg() and log_error() functions will be revised to write the output to syslog()
Posted
Updated 14-Jan-21 20:26pm
v2
Comments
k5054 14-Jan-21 20:40pm    
Have you considered using rsyslog to send the logs to a central server. That way you would not have to visit various hosts to get the logs. Googling for "linux syslog remote servier" should get you various hits.
The Other John Ingram 14-Jan-21 20:47pm    
i generally leave the syslog alone a make it just for system problems.
i create a general log file that has what i need in it to track my system.
i always include exceptions when they mean something.
i create audit files when i find a problem and i want a lot of data to track problems. These audit files mostly have a short life. The general log file lasts as little as one day to a week depending on how much logging goes into it.
The problem with log files is the amount of data that you have to wade through.
Its the old looking at the forest and not the trees you are overwhelmed.

so only put into your log files what you need. yeah i know it sounds simpler than it is.

Since you have more than one server to look at i would gather up the log files and email them to a central pc.
I would also look at log aggregators. Programs that allow you to sift through you log files looking for key messages. Sort of separating the wheat from the chaff.

just a few thoughts.

1 solution

You could use one of these tools: best-log-management-aggregation-monitoring-tools[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900