package sgc.portal.log.service;

import sgc.portal.base.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * <pre>
 * 시스템명   : Common
 * 프로그램ID :
 * 프로그램명  :
 * 설명 :
 * </pre>
 *
 * @author 이범근
 * @version 2020.03.19
 * @see kr.co.sgc.com.log.service
 * @since JDK1.8
 */
@Service
@Slf4j
public class LogFileLoadService {

    @Value("${LOG_FILE}")
    private String logFile;

    public StringBuffer getLog(long startWith){
        StringBuffer retBuffer = new StringBuffer();

        try(BufferedReader buffer = Files.newBufferedReader(Paths.get(logFile), StandardCharsets.UTF_8)) {
            int cnt = 0;
            for (String line1 = buffer.readLine(); line1 != null; line1 = buffer.readLine()) {
                cnt ++;
                if(cnt > startWith){
                    retBuffer.append(line1).append("\n");
                }
            }
        }catch(Exception e){
            log.error(e.getMessage());
            throw new BusinessException("로그파일을 찾을수 없습니다.!");
        }
        return retBuffer;
    }

}
